Skip to content

Commit

Permalink
Updated VCL error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ulises-jeremias committed Aug 26, 2023
1 parent e179bac commit a3930df
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 45 deletions.
6 changes: 3 additions & 3 deletions vcl/buffer.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn (d &Device) buffer(size int) !&Buffer {
mut ret := 0
buffer := cl_create_buffer(d.ctx, mem_read_write, usize(size), unsafe { nil }, &ret)
if ret != success {
return vcl_error(ret)
return error_from_code(ret)
}
if isnil(buffer) {
return err_unknown
Expand All @@ -40,14 +40,14 @@ fn (b &Buffer) load(size int, ptr voidptr) chan IError {
ret := cl_enqueue_write_buffer(b.device.queue, b.memobj, false, 0, usize(size), ptr,
0, unsafe { nil }, &event)
if ret != success {
ch <- vcl_error(ret)
ch <- error_from_code(ret)
return ch
}
spawn fn (event &ClEvent, ch chan IError) {
defer {
cl_release_event(event)
}
ch <- vcl_error(cl_wait_for_events(1, event))
ch <- error_from_code(cl_wait_for_events(1, event))
}(&event, ch)

return ch
Expand Down
2 changes: 1 addition & 1 deletion vcl/bytes.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn (b &Bytes) data() ![]u8 {
ret := cl_enqueue_read_buffer(b.buf.device.queue, b.buf.memobj, true, 0, usize(b.buf.size),
unsafe { &data[0] }, 0, unsafe { nil }, unsafe { nil })
if ret != success {
return vcl_error(ret)
return error_from_code(ret)
}
return data
}
Expand Down
8 changes: 4 additions & 4 deletions vcl/device.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ pub fn (mut d Device) release() ! {
return vcl_error(cl_release_device(d.id))
}

fn (d &Device) get_info_str(param ClDeviceInfo, panic_on_error bool) !string {
fn (d &Device) get_info_str(param ClDeviceInfo, should_panic_on_error bool) !string {
mut info_bytes := [1024]u8{}
mut info_bytes_size := usize(0)
code := cl_get_device_info(d.id, param, 1024, &info_bytes[0], &info_bytes_size)
if code != success {
if panic_on_error {
vcl_panic(code)
if should_panic_on_error {
panic_on_error(code)
}
return vcl_error(code)
return error_or_default(code, '')
}

res := info_bytes[..int(info_bytes_size)].bytestr()
Expand Down
40 changes: 34 additions & 6 deletions vcl/errors.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ const (
pub type ErrVCL = int

pub fn (e ErrVCL) err() IError {
if e == vcl.success {
return none
}

err := match e {
vcl.success { '' }
vcl.device_not_found { vcl.err_device_not_found }
vcl.device_not_available { vcl.err_device_not_available }
vcl.compiler_not_available { vcl.err_compiler_not_available }
Expand Down Expand Up @@ -67,16 +70,41 @@ pub fn (e ErrVCL) err() IError {
return error_with_code(err, int(e))
}

pub fn vcl_error(code int) IError {
pub fn error_from_code(code int) IError {
return ErrVCL(code).err()
}

pub fn error_or_default[T](code int, default T) !T {
if code == vcl.success {
return none
return default
}
return ErrVCL(code).err()
}

pub fn vcl_panic(code int) {
if code != vcl.success {
panic(ErrVCL(code).err())
pub fn typed_error[T](code int) !T {
if code == vcl.success {
return
}
return ErrVCL(code).err()
}

pub fn vcl_error(code int) ! {
err := ErrVCL(code).err()
match err {
none {}
else {
return err
}
}
}

pub fn panic_on_error(code int) {
err := ErrVCL(code).err()
match err {
none {}
else {
panic(err)
}
}
}

Expand Down
15 changes: 4 additions & 11 deletions vcl/image.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn (d &Device) create_image_2d(image_type ImageChannelOrder, bounds Rect, data v
memobj := cl_create_image2d(d.ctx, flags, format, usize(bounds.width), usize(bounds.height),
usize(row_pitch), data, &ret)
if ret != success {
return vcl_error(ret)
return error_from_code(ret)
}

if isnil(memobj) {
Expand Down Expand Up @@ -106,10 +106,7 @@ pub fn (image &Image) data_2d() ![]u8 {
result := []u8{len: image.buf.size, cap: image.buf.size}
ret := cl_enqueue_read_image(image.buf.device.queue, image.buf.memobj, true, origin,
region, 0, 0, unsafe { &result[0] }, 0, unsafe { nil }, unsafe { nil })
if ret != success {
return vcl_error(ret)
}
return result
return error_or_default(ret, result)
}

fn (image &Image) write_queue() !int {
Expand All @@ -123,11 +120,7 @@ fn (image &Image) write_queue() !int {

ret := cl_enqueue_write_image(image.buf.device.queue, image.buf.memobj, true, origin,
region, 0, 0, image.img_data, 0, unsafe { nil }, unsafe { nil })
if ret != success {
println(vcl_error(ret))
return vcl_error(ret)
}
return ret
return error_or_default(ret, ret)
}

// image_general allocates an image buffer TODO not accomplish - broken
Expand Down Expand Up @@ -167,7 +160,7 @@ fn (d &Device) create_image_general(image_type ImageChannelOrder, bounds Rect, r

memobj := cl_create_image(d.ctx, flags, format, desc, data, &ret)
if ret != success {
return vcl_error(ret)
return error_from_code(ret)
}

if isnil(memobj) {
Expand Down
6 changes: 3 additions & 3 deletions vcl/kernel.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn (d &Device) kernel(name string) !&Kernel {
continue
}
if ret != success {
return vcl_error(ret)
return error_from_code(ret)
}
break
}
Expand Down Expand Up @@ -222,7 +222,7 @@ fn (k &Kernel) call(work_sizes []int, lokal_sizes []int) chan IError {
unsafe { &global_work_size_ptr[0] }, unsafe { &local_work_size_ptr[0] }, 0, unsafe { nil },
unsafe { &event })
if res != success {
err := vcl_error(res)
err := error_from_code(res)
ch <- err
return ch
}
Expand All @@ -231,7 +231,7 @@ fn (k &Kernel) call(work_sizes []int, lokal_sizes []int) chan IError {
cl_release_event(event)
}
res := cl_wait_for_events(1, unsafe { &event })
ch <- vcl_error(res)
ch <- error_from_code(res)
}(ch, event)
return ch
}
20 changes: 7 additions & 13 deletions vcl/vcl.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ pub fn get_devices(device_type DeviceType) ![]&Device {
mut ret := cl_get_device_i_ds(p, ClDeviceType(device_type), 0, unsafe { nil },
&n)
if ret != success {
return vcl_error(ret)
return error_from_code(ret)
}
mut device_ids := []ClDeviceId{len: int(n)}
ret = cl_get_device_i_ds(p, ClDeviceType(device_type), n, unsafe { &device_ids[0] },
unsafe { nil })
if ret != success {
return vcl_error(ret)
return error_from_code(ret)
}
for d in device_ids {
device := new_device(d)!
Expand All @@ -34,7 +34,7 @@ pub fn get_default_device() !&Device {
ret := cl_get_device_i_ds(unsafe { &platform_ids[0] }, ClDeviceType(DeviceType.default_device),
1, &id, unsafe { nil })
if ret != success {
return vcl_error(ret)
return error_from_code(ret)
}
return new_device(id)
}
Expand All @@ -43,14 +43,11 @@ fn get_platforms() ![]ClPlatformId {
mut n := u32(0)
mut ret := cl_get_platform_i_ds(0, unsafe { nil }, &n)
if ret != success {
return vcl_error(ret)
return error_from_code(ret)
}
mut platform_ids := []ClPlatformId{len: int(n)}
ret = cl_get_platform_i_ds(n, unsafe { &platform_ids[0] }, unsafe { nil })
if ret != success {
return vcl_error(ret)
}
return platform_ids
return error_or_default(ret, platform_ids)
}

fn new_device(id ClDeviceId) !&Device {
Expand All @@ -61,7 +58,7 @@ fn new_device(id ClDeviceId) !&Device {
d.ctx = cl_create_context(unsafe { nil }, 1, &id, unsafe { nil }, unsafe { nil },
&ret)
if ret != success {
return vcl_error(ret)
return error_from_code(ret)
}
if isnil(d.ctx) {
return err_unknown
Expand All @@ -72,8 +69,5 @@ fn new_device(id ClDeviceId) !&Device {
} else {
d.queue = cl_create_command_queue(d.ctx, d.id, usize(0), &ret)
}
if ret != success {
return vcl_error(ret)
}
return d
return error_or_default(ret, d)
}
5 changes: 1 addition & 4 deletions vcl/vector.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ pub fn (v &Vector[T]) data() ![]T {
mut data := []T{len: int(v.buf.size / int(sizeof(T)))}
ret := cl_enqueue_read_buffer(v.buf.device.queue, v.buf.memobj, true, 0, usize(v.buf.size),
unsafe { &data[0] }, 0, unsafe { nil }, unsafe { nil })
if ret != success {
return vcl_error(ret)
}
return data
return error_or_default(ret, data)
}

// map applies an map kernel on all elements of the vector
Expand Down

0 comments on commit a3930df

Please sign in to comment.