1 /*
2  * Copyright (c) 2016-2021, The OSKAR Developers.
3  * See the LICENSE file at the top-level directory of this distribution.
4  */
5 
6 #include "imager/private_imager.h"
7 #include "imager/oskar_imager.h"
8 
9 #include "imager/private_imager_init_fft.h"
10 #include "imager/oskar_grid_functions_spheroidal.h"
11 #include "imager/oskar_grid_functions_pillbox.h"
12 #include "utility/oskar_device.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
oskar_imager_init_fft(oskar_Imager * h,int * status)18 void oskar_imager_init_fft(oskar_Imager* h, int* status)
19 {
20     oskar_Mem* tmp = 0;
21     if (*status) return;
22 
23     /* Generate the convolution function. */
24     tmp = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU,
25             h->oversample * (h->support + 1), status);
26     switch (h->kernel_type)
27     {
28     case 'S':
29         oskar_grid_convolution_function_spheroidal(h->support, h->oversample,
30                 oskar_mem_double(tmp, status));
31         break;
32     case 'P':
33         oskar_grid_convolution_function_pillbox(h->support, h->oversample,
34                 oskar_mem_double(tmp, status));
35         break;
36     default:
37         *status = OSKAR_ERR_FUNCTION_NOT_AVAILABLE;
38         break;
39     }
40 
41     /* Save the convolution function with appropriate numerical precision. */
42     oskar_mem_free(h->conv_func, status);
43     h->conv_func = oskar_mem_convert_precision(tmp, h->imager_prec, status);
44     oskar_mem_free(tmp, status);
45 
46     /* Copy to device memory if required. */
47     if (h->grid_on_gpu && h->num_devices > 0)
48     {
49         int i = 0;
50         if (h->num_devices < h->num_gpus)
51         {
52             oskar_imager_set_num_devices(h, h->num_gpus);
53         }
54         for (i = 0; i < h->num_gpus; ++i)
55         {
56             DeviceData* d = &h->d[i];
57             oskar_device_set(h->dev_loc, h->gpu_ids[i], status);
58             if (*status) break;
59             oskar_mem_free(d->conv_func, status);
60             d->conv_func = oskar_mem_create_copy(h->conv_func,
61                     h->dev_loc, status);
62         }
63     }
64 }
65 
66 #ifdef __cplusplus
67 }
68 #endif
69