From d545f4f23c7bb31b016ea5935dfd10c4e761b5ac Mon Sep 17 00:00:00 2001 From: Thomas White Date: Fri, 14 Nov 2014 16:23:20 +0100 Subject: pattern_sim: Better error trapping --- src/diffraction-gpu.c | 70 +++++++++++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 30 deletions(-) (limited to 'src/diffraction-gpu.c') diff --git a/src/diffraction-gpu.c b/src/diffraction-gpu.c index e31d7b0f..bdfdb13b 100644 --- a/src/diffraction-gpu.c +++ b/src/diffraction-gpu.c @@ -172,15 +172,15 @@ static int set_arg_mem(struct gpu_context *gctx, int idx, cl_mem val) } -static void do_panels(struct gpu_context *gctx, struct image *image, +static int do_panels(struct gpu_context *gctx, struct image *image, double k, double weight, int *n_inf, int *n_neg, int *n_nan) { int i; const int sampling = 4; /* This, squared, number of samples / pixel */ - if ( set_arg_float(gctx, 1, k) ) return; - if ( set_arg_float(gctx, 2, weight) ) return; + if ( set_arg_float(gctx, 1, k) ) return 1; + if ( set_arg_float(gctx, 2, weight) ) return 1; /* Iterate over panels */ for ( i=0; idet->n_panels; i++ ) { @@ -206,20 +206,20 @@ static void do_panels(struct gpu_context *gctx, struct image *image, diff_size, NULL, &err); if ( err != CL_SUCCESS ) { ERROR("Couldn't allocate diffraction memory\n"); - return; + return 1; } - if ( set_arg_mem(gctx, 0, diff) ) return; + if ( set_arg_mem(gctx, 0, diff) ) return 1; - if ( set_arg_int(gctx, 3, pan_width) ) return; - if ( set_arg_float(gctx, 4, p->cnx) ) return; - if ( set_arg_float(gctx, 5, p->cny) ) return; - if ( set_arg_float(gctx, 6, p->fsx) ) return; - if ( set_arg_float(gctx, 7, p->fsy) ) return; - if ( set_arg_float(gctx, 8, p->ssx) ) return; - if ( set_arg_float(gctx, 9, p->ssy) ) return; - if ( set_arg_float(gctx, 10, p->res) ) return; - if ( set_arg_float(gctx, 11, p->clen) ) return; + if ( set_arg_int(gctx, 3, pan_width) ) return 1; + if ( set_arg_float(gctx, 4, p->cnx) ) return 1; + if ( set_arg_float(gctx, 5, p->cny) ) return 1; + if ( set_arg_float(gctx, 6, p->fsx) ) return 1; + if ( set_arg_float(gctx, 7, p->fsy) ) return 1; + if ( set_arg_float(gctx, 8, p->ssx) ) return 1; + if ( set_arg_float(gctx, 9, p->ssy) ) return 1; + if ( set_arg_float(gctx, 10, p->res) ) return 1; + if ( set_arg_float(gctx, 11, p->clen) ) return 1; dims[0] = pan_width * sampling; dims[1] = pan_height * sampling; @@ -231,7 +231,7 @@ static void do_panels(struct gpu_context *gctx, struct image *image, sampling*sampling*sizeof(cl_float), NULL); if ( err != CL_SUCCESS ) { ERROR("Couldn't set local memory: %s\n", clError(err)); - return; + return 1; } err = clEnqueueNDRangeKernel(gctx->cq, gctx->kern, 2, NULL, @@ -239,7 +239,7 @@ static void do_panels(struct gpu_context *gctx, struct image *image, if ( err != CL_SUCCESS ) { ERROR("Couldn't enqueue diffraction kernel: %s\n", clError(err)); - return; + return 1; } clFinish(gctx->cq); @@ -250,7 +250,7 @@ static void do_panels(struct gpu_context *gctx, struct image *image, if ( err != CL_SUCCESS ) { ERROR("Couldn't map diffraction buffer: %s\n", clError(err)); - return; + return 1; } for ( fs=0; fskern, 12, sizeof(cl_float16), &cell); if ( err != CL_SUCCESS ) { ERROR("Couldn't set unit cell: %s\n", clError(err)); - return; + return 1; } - if ( set_arg_mem(gctx, 13, gctx->intensities) ) return; - if ( set_arg_mem(gctx, 14, gctx->flags) ) return; - if ( set_arg_mem(gctx, 15, gctx->sinc_luts[na-1]) ) return; - if ( set_arg_mem(gctx, 16, gctx->sinc_luts[nb-1]) ) return; - if ( set_arg_mem(gctx, 17, gctx->sinc_luts[nc-1]) ) return; + if ( set_arg_mem(gctx, 13, gctx->intensities) ) return 1; + if ( set_arg_mem(gctx, 14, gctx->flags) ) return 1; + if ( set_arg_mem(gctx, 15, gctx->sinc_luts[na-1]) ) return 1; + if ( set_arg_mem(gctx, 16, gctx->sinc_luts[nb-1]) ) return 1; + if ( set_arg_mem(gctx, 17, gctx->sinc_luts[nc-1]) ) return 1; /* Allocate memory for the result */ image->data = calloc(image->width * image->height, sizeof(float)); + if ( image->data == NULL ) { + ERROR("Couldn't allocate memory for result.\n"); + return 1; + } double tot = 0.0; for ( i=0; insamples; i++ ) { @@ -333,9 +339,11 @@ void get_diffraction_gpu(struct gpu_context *gctx, struct image *image, ph_lambda_to_eV(1.0/image->spectrum[i].k), image->spectrum[i].weight); - do_panels(gctx, image, image->spectrum[i].k, - image->spectrum[i].weight, - &n_inf, &n_neg, &n_nan); + err = do_panels(gctx, image, image->spectrum[i].k, + image->spectrum[i].weight, + &n_inf, &n_neg, &n_nan); + + if ( err ) return 1; tot += image->spectrum[i].weight; @@ -359,6 +367,8 @@ void get_diffraction_gpu(struct gpu_context *gctx, struct image *image, } } + + return 0; } -- cgit v1.2.3