ufo-anka-backproject-task.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. /*
  2. * Copyright (C) 2011-2014 Karlsruhe Institute of Technology
  3. *
  4. * This file is part of Ufo.
  5. *
  6. * This library is free software: you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation, either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdio.h>
  20. #include <math.h>
  21. #include <glib.h>
  22. #include <glib/gprintf.h>
  23. #ifdef __APPLE__
  24. #include <OpenCL/cl.h>
  25. #else
  26. #include <CL/cl.h>
  27. #endif
  28. #include "ufo-anka-backproject-task.h"
  29. /* Copy only neccessary projection region */
  30. /* TODO: make this a parameter? */
  31. #define COPY_PROJECTION_REGION 1
  32. #define EXTRACT_INT(region, index) g_value_get_int (g_value_array_get_nth ((region), (index)))
  33. #define EXTRACT_FLOAT(region, index) g_value_get_float (g_value_array_get_nth ((region), (index)))
  34. #define REGION_SIZE(region) ((EXTRACT_INT ((region), 2)) == 0) ? 0 : \
  35. ((EXTRACT_INT ((region), 1) - EXTRACT_INT ((region), 0) - 1) /\
  36. EXTRACT_INT ((region), 2) + 1)
  37. #define PAD_TO_DIVIDE(dividend, divisor) ((dividend) + (divisor) - (dividend) % (divisor))
  38. /**
  39. * SECTION:ufo-anka-backproject-task
  40. * @Short_description: Backproject projection by projection
  41. * @Title: anka_backproject
  42. *
  43. */
  44. typedef enum {
  45. PARAM_Z,
  46. PARAM_CENTER,
  47. PARAM_LAMINO
  48. } Param;
  49. struct _UfoAnkaBackprojectTaskPrivate {
  50. /* private */
  51. gboolean generated;
  52. guint count;
  53. /* sine and cosine table size based on BURST */
  54. gsize table_size;
  55. /* OpenCL */
  56. cl_context context;
  57. cl_kernel vector_kernel;
  58. cl_kernel scalar_kernel;
  59. cl_sampler sampler;
  60. /* Buffered images for invoking backprojection on BURST projections at once.
  61. * We potentially don't need to copy the last image and can use the one from
  62. * framework directly but it seems to have no performance effects. */
  63. cl_mem images[BURST];
  64. /* properties */
  65. GValueArray *x_region;
  66. GValueArray *y_region;
  67. GValueArray *region;
  68. GValueArray *center;
  69. GValueArray *projection_offset;
  70. float sines[BURST], cosines[BURST];
  71. guint num_projections;
  72. gfloat overall_angle;
  73. gfloat tomo_angle;
  74. gfloat lamino_angle;
  75. gfloat z;
  76. Param parameter;
  77. };
  78. static void ufo_task_interface_init (UfoTaskIface *iface);
  79. G_DEFINE_TYPE_WITH_CODE (UfoAnkaBackprojectTask, ufo_anka_backproject_task, UFO_TYPE_TASK_NODE,
  80. G_IMPLEMENT_INTERFACE (UFO_TYPE_TASK,
  81. ufo_task_interface_init))
  82. #define UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), UFO_TYPE_ANKA_BACKPROJECT_TASK, UfoAnkaBackprojectTaskPrivate))
  83. enum {
  84. PROP_0,
  85. PROP_X_REGION,
  86. PROP_Y_REGION,
  87. PROP_Z,
  88. PROP_REGION,
  89. PROP_PROJECTION_OFFSET,
  90. PROP_CENTER,
  91. PROP_NUM_PROJECTIONS,
  92. PROP_OVERALL_ANGLE,
  93. PROP_TOMO_ANGLE,
  94. PROP_LAMINO_ANGLE,
  95. PROP_PARAMETER,
  96. N_PROPERTIES
  97. };
  98. static GParamSpec *properties[N_PROPERTIES] = { NULL, };
  99. static inline void
  100. swap (gint *first, gint *second) {
  101. gint tmp;
  102. tmp = *first;
  103. *first = *second;
  104. *second = tmp;
  105. }
  106. /**
  107. * Determine the left and right column to read from a projection at a given
  108. * tomographic angle.
  109. */
  110. static void
  111. determine_x_extrema (gfloat extrema[2], GValueArray *x_extrema, GValueArray *y_extrema,
  112. gfloat tomo_angle, gfloat x_center)
  113. {
  114. gfloat sin_tomo, cos_tomo;
  115. gint x_min, x_max, y_min, y_max;
  116. sin_tomo = sin (tomo_angle);
  117. cos_tomo = cos (tomo_angle);
  118. x_min = EXTRACT_INT (x_extrema, 0);
  119. /* The interval is right-opened when OpenCL indices for both x and y are generated, */
  120. /* so the last index doesn't count */
  121. x_max = EXTRACT_INT (x_extrema, 1) - 1;
  122. y_min = EXTRACT_INT (y_extrema, 0);
  123. y_max = EXTRACT_INT (y_extrema, 1) - 1;
  124. if (sin_tomo < 0) {
  125. swap (&y_min, &y_max);
  126. }
  127. if (cos_tomo < 0) {
  128. swap (&x_min, &x_max);
  129. }
  130. extrema[0] = cos_tomo * x_min + sin_tomo * y_min + x_center;
  131. /* +1 because extrema[1] will be accessed by interpolation
  132. * but the region in copying is right-open */
  133. extrema[1] = cos_tomo * x_max + sin_tomo * y_max + x_center + 1;
  134. }
  135. /**
  136. * Determine the top and bottom row to read from a projection at given
  137. * tomographic and laminographic angles.
  138. */
  139. static void
  140. determine_y_extrema (gfloat extrema[2], GValueArray *x_extrema, GValueArray *y_extrema,
  141. gfloat z_extrema[2], gfloat tomo_angle, gfloat lamino_angle,
  142. gfloat y_center)
  143. {
  144. gfloat sin_tomo, cos_tomo, sin_lamino, cos_lamino;
  145. gint x_min, x_max, y_min, y_max;
  146. sin_tomo = sin (tomo_angle);
  147. cos_tomo = cos (tomo_angle);
  148. sin_lamino = sin (lamino_angle);
  149. cos_lamino = cos (lamino_angle);
  150. x_min = EXTRACT_INT (x_extrema, 0);
  151. x_max = EXTRACT_INT (x_extrema, 1) - 1;
  152. y_min = EXTRACT_INT (y_extrema, 0);
  153. y_max = EXTRACT_INT (y_extrema, 1) - 1;
  154. if (sin_tomo < 0) {
  155. swap (&x_min, &x_max);
  156. }
  157. if (cos_tomo > 0) {
  158. swap (&y_min, &y_max);
  159. }
  160. extrema[0] = sin_tomo * x_min - cos_tomo * y_min;
  161. extrema[1] = sin_tomo * x_max - cos_tomo * y_max;
  162. extrema[0] = extrema[0] * cos_lamino + z_extrema[0] * sin_lamino + y_center;
  163. extrema[1] = extrema[1] * cos_lamino + z_extrema[1] * sin_lamino + y_center + 1;
  164. }
  165. /**
  166. * clip:
  167. * @result: resulting clipped extrema
  168. * @extrema: (min, max)
  169. * @maximum: projection width or height
  170. *
  171. * Clip extrema to an allowed interval [0, projection width/height)
  172. */
  173. static void
  174. clip (gint result[2], gfloat extrema[2], gint maximum)
  175. {
  176. result[0] = (gint) floorf (extrema[0]);
  177. result[1] = (gint) ceilf (extrema[1]);
  178. if (result[0] < 0) {
  179. result[0] = 0;
  180. }
  181. if (result[0] > maximum) {
  182. result[0] = maximum;
  183. }
  184. if (result[1] < 0) {
  185. result[1] = 0;
  186. }
  187. if (result[1] > maximum) {
  188. result[1] = maximum;
  189. }
  190. if (result[0] == result[1]) {
  191. if (result[1] < maximum) {
  192. result[1]++;
  193. } else if (result[0] > 0) {
  194. result[0]--;
  195. } else {
  196. g_warning ("Cannot extend");
  197. }
  198. } else if (result[0] > result[1]) {
  199. g_warning ("Invalid extrema: minimum larger than maximum");
  200. }
  201. }
  202. /**
  203. * Determine the left and right column to read from a projection at a given
  204. * tomographic angle. The result is bound to [0, projection width)
  205. */
  206. static void
  207. determine_x_region (gint result[2], GValueArray *x_extrema, GValueArray *y_extrema, gfloat tomo_angle,
  208. gfloat x_center, gint width)
  209. {
  210. gfloat extrema[2];
  211. determine_x_extrema (extrema, x_extrema, y_extrema, tomo_angle, x_center);
  212. clip (result, extrema, width);
  213. }
  214. /**
  215. * Determine the top and bottom column to read from a projection at given
  216. * tomographic and laminographic angles. The result is bound to
  217. * [0, projection height).
  218. */
  219. static void
  220. determine_y_region (gint result[2], GValueArray *x_extrema, GValueArray *y_extrema, gfloat z_extrema[2],
  221. gfloat tomo_angle, gfloat lamino_angle, gfloat y_center, gint height)
  222. {
  223. gfloat extrema[2];
  224. determine_y_extrema (extrema, x_extrema, y_extrema, z_extrema, tomo_angle,
  225. lamino_angle, y_center);
  226. clip (result, extrema, height);
  227. }
  228. static void
  229. set_region (GValueArray *src, GValueArray **dst)
  230. {
  231. if (EXTRACT_INT (src, 0) > EXTRACT_INT (src, 1)) {
  232. g_log ("Ufo", G_LOG_LEVEL_CRITICAL,
  233. "Error <%s:%i>: Invalid region [\"from\", \"to\", \"step\"]: [%d, %d, %d], "\
  234. "\"from\" has to be less than or equal to \"to\"",
  235. __FILE__, __LINE__,
  236. EXTRACT_INT (src, 0), EXTRACT_INT (src, 1), EXTRACT_INT (src, 2));
  237. }
  238. else {
  239. g_value_array_free (*dst);
  240. *dst = g_value_array_copy (src);
  241. }
  242. }
  243. static void
  244. copy_to_image (UfoBuffer *input,
  245. cl_mem output_image,
  246. cl_command_queue cmd_queue,
  247. size_t origin[3],
  248. size_t region[3],
  249. gint in_width)
  250. {
  251. const UfoBufferLocation location = ufo_buffer_get_location (input);
  252. cl_mem input_data;
  253. gfloat *input_data_host;
  254. size_t src_offset;
  255. switch (location) {
  256. case UFO_BUFFER_LOCATION_HOST:
  257. input_data_host = ufo_buffer_get_host_array (input, NULL);
  258. src_offset = origin[1] * in_width + origin[0];
  259. UFO_RESOURCES_CHECK_CLERR (clEnqueueWriteImage (cmd_queue,
  260. output_image,
  261. CL_TRUE,
  262. origin,
  263. region,
  264. 0,
  265. 0,
  266. input_data_host + src_offset,
  267. 0,
  268. NULL,
  269. NULL));
  270. break;
  271. case UFO_BUFFER_LOCATION_DEVICE:
  272. input_data = ufo_buffer_get_device_array (input, cmd_queue);
  273. src_offset = (origin[1] * in_width + origin[0]) * sizeof (cl_float);
  274. UFO_RESOURCES_CHECK_CLERR (clEnqueueCopyBufferToImage (cmd_queue,
  275. input_data,
  276. output_image,
  277. src_offset,
  278. origin,
  279. region,
  280. 0,
  281. NULL,
  282. NULL));
  283. break;
  284. case UFO_BUFFER_LOCATION_DEVICE_IMAGE:
  285. input_data = ufo_buffer_get_device_image (input, cmd_queue);
  286. UFO_RESOURCES_CHECK_CLERR (clEnqueueCopyImage (cmd_queue,
  287. input_data,
  288. output_image,
  289. origin,
  290. origin,
  291. region,
  292. 0,
  293. NULL,
  294. NULL));
  295. break;
  296. default:
  297. g_warning ("Invalid input buffer location");
  298. break;
  299. }
  300. }
  301. UfoNode *
  302. ufo_anka_backproject_task_new (void)
  303. {
  304. return UFO_NODE (g_object_new (UFO_TYPE_ANKA_BACKPROJECT_TASK, NULL));
  305. }
  306. static void
  307. ufo_anka_backproject_task_setup (UfoTask *task,
  308. UfoResources *resources,
  309. GError **error)
  310. {
  311. UfoAnkaBackprojectTaskPrivate *priv;
  312. cl_int cl_error;
  313. gint i;
  314. gchar *vector_kernel_name, *kernel_filename;
  315. vector_kernel_name = g_strdup_printf ("backproject_burst_%d", BURST);
  316. if (!vector_kernel_name) {
  317. g_warning ("Error making burst kernel name");
  318. }
  319. priv = UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE (task);
  320. priv->context = ufo_resources_get_context (resources);
  321. switch (priv->parameter) {
  322. case PARAM_Z:
  323. kernel_filename = g_strdup ("z_kernel.cl");
  324. break;
  325. case PARAM_CENTER:
  326. kernel_filename = g_strdup ("center_kernel.cl");
  327. break;
  328. case PARAM_LAMINO:
  329. kernel_filename = g_strdup ("lamino_kernel.cl");
  330. break;
  331. default:
  332. g_warning ("Unkown varying parameter");
  333. break;
  334. }
  335. priv->vector_kernel = ufo_resources_get_kernel (resources, kernel_filename,
  336. vector_kernel_name, error);
  337. priv->scalar_kernel = ufo_resources_get_kernel (resources, kernel_filename,
  338. "backproject_burst_1", error);
  339. priv->sampler = clCreateSampler (priv->context,
  340. (cl_bool) FALSE,
  341. CL_ADDRESS_CLAMP,
  342. CL_FILTER_LINEAR,
  343. &cl_error);
  344. UFO_RESOURCES_CHECK_CLERR (clRetainContext (priv->context));
  345. UFO_RESOURCES_CHECK_CLERR (cl_error);
  346. if (priv->vector_kernel) {
  347. UFO_RESOURCES_CHECK_CLERR (clRetainKernel (priv->vector_kernel));
  348. }
  349. if (priv->scalar_kernel) {
  350. UFO_RESOURCES_CHECK_CLERR (clRetainKernel (priv->scalar_kernel));
  351. }
  352. for (i = 0; i < BURST; i++) {
  353. priv->images[i] = NULL;
  354. }
  355. switch (BURST) {
  356. case 1: priv->table_size = sizeof (cl_float); break;
  357. case 2: priv->table_size = sizeof (cl_float2); break;
  358. case 4: priv->table_size = sizeof (cl_float4); break;
  359. case 8: priv->table_size = sizeof (cl_float8); break;
  360. case 16: priv->table_size = sizeof (cl_float16); break;
  361. default: g_warning ("Unsupported vector size"); break;
  362. }
  363. g_free (vector_kernel_name);
  364. g_free (kernel_filename);
  365. }
  366. static void
  367. ufo_anka_backproject_task_get_requisition (UfoTask *task,
  368. UfoBuffer **inputs,
  369. UfoRequisition *requisition)
  370. {
  371. UfoAnkaBackprojectTaskPrivate *priv;
  372. gfloat start, stop, step;
  373. priv = UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE (task);
  374. start = EXTRACT_FLOAT (priv->region, 0);
  375. stop = EXTRACT_FLOAT (priv->region, 1);
  376. step = EXTRACT_FLOAT (priv->region, 2);
  377. if (!priv->num_projections) {
  378. g_warning ("Number of projections has not been set");
  379. }
  380. if (step == 0.0f) {
  381. g_warning ("Step in region is 0");
  382. }
  383. requisition->n_dims = 3;
  384. requisition->dims[0] = REGION_SIZE (priv->x_region);
  385. requisition->dims[1] = REGION_SIZE (priv->y_region);
  386. requisition->dims[2] = (gint) ceil ((stop - start) / step);
  387. }
  388. static guint
  389. ufo_anka_backproject_task_get_num_inputs (UfoTask *task)
  390. {
  391. return 1;
  392. }
  393. static guint
  394. ufo_anka_backproject_task_get_num_dimensions (UfoTask *task,
  395. guint input)
  396. {
  397. g_return_val_if_fail (input == 0, 0);
  398. return 3;
  399. }
  400. static gboolean
  401. ufo_anka_backproject_task_equal_real (UfoNode *n1,
  402. UfoNode *n2)
  403. {
  404. g_return_val_if_fail (UFO_IS_ANKA_BACKPROJECT_TASK (n1) && UFO_IS_ANKA_BACKPROJECT_TASK (n2), FALSE);
  405. return UFO_ANKA_BACKPROJECT_TASK (n1)->priv->vector_kernel == UFO_ANKA_BACKPROJECT_TASK (n2)->priv->vector_kernel;
  406. }
  407. static UfoTaskMode
  408. ufo_anka_backproject_task_get_mode (UfoTask *task)
  409. {
  410. return UFO_TASK_MODE_REDUCTOR | UFO_TASK_MODE_GPU;
  411. }
  412. static gboolean
  413. ufo_anka_backproject_task_process (UfoTask *task,
  414. UfoBuffer **inputs,
  415. UfoBuffer *output,
  416. UfoRequisition *requisition)
  417. {
  418. UfoAnkaBackprojectTaskPrivate *priv;
  419. UfoRequisition in_req;
  420. UfoGpuNode *node;
  421. UfoProfiler *profiler;
  422. gfloat tomo_angle, *sines, *cosines;
  423. gint i, index;
  424. gint cumulate;
  425. gsize table_size;
  426. gboolean scalar;
  427. /* regions stripped off the "to" value */
  428. gfloat x_region[2], y_region[2], z_region[2], x_center[2], z_ends[2], lamino_angles[2], y_center, sin_lamino, cos_lamino;
  429. gint x_copy_region[2], y_copy_region[2];
  430. cl_kernel kernel;
  431. cl_command_queue cmd_queue;
  432. cl_mem out_mem;
  433. cl_int cl_error;
  434. /* image creation and copying */
  435. cl_image_format image_fmt;
  436. size_t origin[3];
  437. size_t region[3];
  438. /* keep the warp size satisfied but make sure the local grid is localized
  439. * around a point in 3D for efficient caching */
  440. const gint real_size[4] = {requisition->dims[0], requisition->dims[1], requisition->dims[2], 0};
  441. const gsize local_work_size[] = {16, 8, 8};
  442. gsize global_work_size[3];
  443. global_work_size[0] = requisition->dims[0] % local_work_size[0] ?
  444. PAD_TO_DIVIDE (requisition->dims[0], local_work_size[0]) :
  445. requisition->dims[0];
  446. global_work_size[1] = requisition->dims[1] % local_work_size[1] ?
  447. PAD_TO_DIVIDE (requisition->dims[1], local_work_size[1]) :
  448. requisition->dims[1];
  449. global_work_size[2] = requisition->dims[2] % local_work_size[2] ?
  450. PAD_TO_DIVIDE (requisition->dims[2], local_work_size[2]) :
  451. requisition->dims[2];
  452. priv = UFO_ANKA_BACKPROJECT_TASK (task)->priv;
  453. node = UFO_GPU_NODE (ufo_task_node_get_proc_node (UFO_TASK_NODE (task)));
  454. cmd_queue = ufo_gpu_node_get_cmd_queue (node);
  455. out_mem = ufo_buffer_get_device_array (output, cmd_queue);
  456. ufo_buffer_get_requisition (inputs[0], &in_req);
  457. index = priv->count % BURST;
  458. tomo_angle = priv->tomo_angle > -G_MAXFLOAT ? priv->tomo_angle :
  459. priv->overall_angle * priv->count / priv->num_projections;
  460. priv->sines[index] = sin (tomo_angle);
  461. priv->cosines[index] = cos (tomo_angle);
  462. x_region[0] = (gfloat) EXTRACT_INT (priv->x_region, 0);
  463. x_region[1] = (gfloat) EXTRACT_INT (priv->x_region, 2);
  464. y_region[0] = (gfloat) EXTRACT_INT (priv->y_region, 0);
  465. y_region[1] = (gfloat) EXTRACT_INT (priv->y_region, 2);
  466. if (priv->parameter == PARAM_Z) {
  467. z_ends[0] = z_region[0] = EXTRACT_FLOAT (priv->region, 0);
  468. z_region[1] = EXTRACT_FLOAT (priv->region, 2);
  469. z_ends[1] = EXTRACT_FLOAT (priv->region, 1);
  470. } else {
  471. z_ends[0] = priv->z;
  472. z_ends[1] = priv->z + 1.0f;
  473. }
  474. if (priv->parameter == PARAM_CENTER) {
  475. x_center[0] = EXTRACT_FLOAT (priv->region, 0) - EXTRACT_INT (priv->projection_offset, 0);
  476. x_center[1] = EXTRACT_FLOAT (priv->region, 2);
  477. } else {
  478. x_center[0] = x_center[1] = EXTRACT_FLOAT (priv->center, 0) - EXTRACT_INT (priv->projection_offset, 0);
  479. }
  480. y_center = EXTRACT_FLOAT (priv->center, 1) - EXTRACT_INT (priv->projection_offset, 1);
  481. if (priv->parameter == PARAM_LAMINO) {
  482. lamino_angles[0] = EXTRACT_FLOAT (priv->region, 0);
  483. lamino_angles[1] = EXTRACT_FLOAT (priv->region, 2);
  484. } else {
  485. lamino_angles[0] = lamino_angles[1] = priv->lamino_angle;
  486. }
  487. sin_lamino = sinf (priv->lamino_angle);
  488. cos_lamino = cosf (priv->lamino_angle);
  489. scalar = priv->count >= priv->num_projections / BURST * BURST ? 1 : 0;
  490. /* If COPY_PROJECTION_REGION is True we copy only the part necessary */
  491. /* for a given tomographic and laminographic angle */
  492. /* TODO: Extend the region determination to be able to handle PARAM_LAMINO */
  493. if (COPY_PROJECTION_REGION && priv->parameter != PARAM_LAMINO) {
  494. determine_x_region (x_copy_region, priv->x_region, priv->y_region, tomo_angle,
  495. EXTRACT_FLOAT (priv->center, 0), in_req.dims[0]);
  496. determine_y_region (y_copy_region, priv->x_region, priv->y_region, z_ends,
  497. tomo_angle, priv->lamino_angle, EXTRACT_FLOAT (priv->center, 1),
  498. in_req.dims[1]);
  499. origin[0] = x_copy_region[0];
  500. origin[1] = y_copy_region[0];
  501. origin[2] = 0;
  502. region[0] = x_copy_region[1] - x_copy_region[0];
  503. region[1] = y_copy_region[1] - y_copy_region[0];
  504. } else {
  505. origin[0] = origin[1] = origin[2] = 0;
  506. region[0] = in_req.dims[0];
  507. region[1] = in_req.dims[1];
  508. }
  509. region[2] = 1;
  510. if (priv->images[index] == NULL) {
  511. /* TODO: dangerous, don't rely on the ufo-buffer */
  512. image_fmt.image_channel_order = CL_R;
  513. image_fmt.image_channel_data_type = CL_FLOAT;
  514. /* TODO: what with the "other" API? */
  515. priv->images[index] = clCreateImage2D (priv->context,
  516. CL_MEM_READ_ONLY,
  517. &image_fmt,
  518. in_req.dims[0],
  519. in_req.dims[1],
  520. 0,
  521. NULL,
  522. &cl_error);
  523. UFO_RESOURCES_CHECK_CLERR (cl_error);
  524. }
  525. copy_to_image (inputs[0], priv->images[index], cmd_queue, origin, region, in_req.dims[0]);
  526. if (scalar) {
  527. kernel = priv->scalar_kernel;
  528. cumulate = priv->count;
  529. table_size = sizeof (cl_float);
  530. sines = &priv->sines[index];
  531. cosines = &priv->cosines[index];
  532. i = 1;
  533. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, 0, sizeof (cl_mem), &priv->images[index]));
  534. } else {
  535. kernel = priv->vector_kernel;
  536. cumulate = priv->count + 1 == BURST ? 0 : 1;
  537. table_size = priv->table_size;
  538. sines = priv->sines;
  539. cosines = priv->cosines;
  540. i = BURST;
  541. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, index, sizeof (cl_mem), &priv->images[index]));
  542. }
  543. if (scalar || index == BURST - 1) {
  544. /* Execute the kernel after BURST images have arrived, i.e. we use more
  545. * projections at one invocation, so the number of read/writes to the
  546. * result is reduced by a factor of BURST. If there are not enough
  547. * projecttions left, execute the scalar kernel */
  548. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_mem), &out_mem));
  549. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_sampler), &priv->sampler));
  550. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_int3), real_size));
  551. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float2), x_center));
  552. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float), (cl_float *) &y_center));
  553. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float2), x_region));
  554. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float2), y_region));
  555. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float2), z_region));
  556. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float2), lamino_angles));
  557. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float), &sin_lamino));
  558. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, sizeof (cl_float), &cos_lamino));
  559. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, table_size, sines));
  560. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i++, table_size, cosines));
  561. UFO_RESOURCES_CHECK_CLERR (clSetKernelArg (kernel, i, sizeof (cl_int), (cl_int *) &cumulate));
  562. profiler = ufo_task_node_get_profiler (UFO_TASK_NODE (task));
  563. ufo_profiler_call (profiler, cmd_queue, kernel, 3, global_work_size, local_work_size);
  564. }
  565. priv->count++;
  566. return TRUE;
  567. }
  568. static gboolean
  569. ufo_anka_backproject_task_generate (UfoTask *task,
  570. UfoBuffer *output,
  571. UfoRequisition *requisition)
  572. {
  573. UfoAnkaBackprojectTaskPrivate *priv;
  574. priv = UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE (task);
  575. if (priv->generated) {
  576. return FALSE;
  577. }
  578. priv->generated = TRUE;
  579. return TRUE;
  580. }
  581. static void
  582. ufo_anka_backproject_task_finalize (GObject *object)
  583. {
  584. UfoAnkaBackprojectTaskPrivate *priv;
  585. gint i;
  586. priv = UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE (object);
  587. g_value_array_free (priv->x_region);
  588. g_value_array_free (priv->y_region);
  589. g_value_array_free (priv->region);
  590. g_value_array_free (priv->projection_offset);
  591. g_value_array_free (priv->center);
  592. if (priv->vector_kernel) {
  593. UFO_RESOURCES_CHECK_CLERR (clReleaseKernel (priv->vector_kernel));
  594. priv->vector_kernel = NULL;
  595. }
  596. if (priv->scalar_kernel) {
  597. UFO_RESOURCES_CHECK_CLERR (clReleaseKernel (priv->scalar_kernel));
  598. priv->scalar_kernel = NULL;
  599. }
  600. if (priv->context) {
  601. UFO_RESOURCES_CHECK_CLERR (clReleaseContext (priv->context));
  602. priv->context = NULL;
  603. }
  604. if (priv->sampler) {
  605. UFO_RESOURCES_CHECK_CLERR (clReleaseSampler (priv->sampler));
  606. priv->sampler = NULL;
  607. }
  608. for (i = 0; i < BURST; i++) {
  609. if (priv->images[i] != NULL) {
  610. UFO_RESOURCES_CHECK_CLERR (clReleaseMemObject (priv->images[i]));
  611. priv->images[i] = NULL;
  612. }
  613. }
  614. G_OBJECT_CLASS (ufo_anka_backproject_task_parent_class)->finalize (object);
  615. }
  616. static void
  617. ufo_task_interface_init (UfoTaskIface *iface)
  618. {
  619. iface->setup = ufo_anka_backproject_task_setup;
  620. iface->get_requisition = ufo_anka_backproject_task_get_requisition;
  621. iface->get_num_inputs = ufo_anka_backproject_task_get_num_inputs;
  622. iface->get_num_dimensions = ufo_anka_backproject_task_get_num_dimensions;
  623. iface->get_mode = ufo_anka_backproject_task_get_mode;
  624. iface->process = ufo_anka_backproject_task_process;
  625. iface->generate = ufo_anka_backproject_task_generate;
  626. }
  627. static void
  628. ufo_anka_backproject_task_set_property (GObject *object,
  629. guint property_id,
  630. const GValue *value,
  631. GParamSpec *pspec)
  632. {
  633. UfoAnkaBackprojectTaskPrivate *priv = UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE (object);
  634. GValueArray *array;
  635. switch (property_id) {
  636. case PROP_X_REGION:
  637. array = (GValueArray *) g_value_get_boxed (value);
  638. set_region (array, &priv->x_region);
  639. break;
  640. case PROP_Y_REGION:
  641. array = (GValueArray *) g_value_get_boxed (value);
  642. set_region (array, &priv->y_region);
  643. break;
  644. case PROP_Z:
  645. priv->z = g_value_get_float (value);
  646. break;
  647. case PROP_REGION:
  648. array = (GValueArray *) g_value_get_boxed (value);
  649. g_value_array_free (priv->region);
  650. priv->region = g_value_array_copy (array);
  651. break;
  652. case PROP_PROJECTION_OFFSET:
  653. array = (GValueArray *) g_value_get_boxed (value);
  654. g_value_array_free (priv->projection_offset);
  655. priv->projection_offset = g_value_array_copy (array);
  656. break;
  657. case PROP_CENTER:
  658. array = (GValueArray *) g_value_get_boxed (value);
  659. g_value_array_free (priv->center);
  660. priv->center = g_value_array_copy (array);
  661. break;
  662. case PROP_NUM_PROJECTIONS:
  663. priv->num_projections = g_value_get_uint (value);
  664. break;
  665. case PROP_OVERALL_ANGLE:
  666. priv->overall_angle = g_value_get_float (value);
  667. break;
  668. case PROP_TOMO_ANGLE:
  669. priv->tomo_angle = g_value_get_float (value);
  670. break;
  671. case PROP_LAMINO_ANGLE:
  672. priv->lamino_angle = g_value_get_float (value);
  673. break;
  674. case PROP_PARAMETER:
  675. if (!g_strcmp0 (g_value_get_string (value), "z")) {
  676. priv->parameter = PARAM_Z;
  677. } else if (!g_strcmp0 (g_value_get_string (value), "x-center")) {
  678. priv->parameter = PARAM_CENTER;
  679. } else if (!g_strcmp0 (g_value_get_string (value), "lamino-angle")) {
  680. priv->parameter = PARAM_LAMINO;
  681. }
  682. break;
  683. default:
  684. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  685. break;
  686. }
  687. }
  688. static void
  689. ufo_anka_backproject_task_get_property (GObject *object,
  690. guint property_id,
  691. GValue *value,
  692. GParamSpec *pspec)
  693. {
  694. UfoAnkaBackprojectTaskPrivate *priv = UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE (object);
  695. switch (property_id) {
  696. case PROP_X_REGION:
  697. g_value_set_boxed (value, priv->x_region);
  698. break;
  699. case PROP_Y_REGION:
  700. g_value_set_boxed (value, priv->y_region);
  701. break;
  702. case PROP_Z:
  703. g_value_set_float (value, priv->z);
  704. break;
  705. case PROP_REGION:
  706. g_value_set_boxed (value, priv->region);
  707. break;
  708. case PROP_PROJECTION_OFFSET:
  709. g_value_set_boxed (value, priv->projection_offset);
  710. break;
  711. case PROP_CENTER:
  712. g_value_set_boxed (value, priv->center);
  713. break;
  714. case PROP_NUM_PROJECTIONS:
  715. g_value_set_uint (value, priv->num_projections);
  716. break;
  717. case PROP_OVERALL_ANGLE:
  718. g_value_set_float (value, priv->overall_angle);
  719. break;
  720. case PROP_TOMO_ANGLE:
  721. g_value_set_float (value, priv->tomo_angle);
  722. break;
  723. case PROP_LAMINO_ANGLE:
  724. g_value_set_float (value, priv->lamino_angle);
  725. break;
  726. case PROP_PARAMETER:
  727. switch (priv->parameter) {
  728. case PARAM_Z:
  729. g_value_set_string (value, "z");
  730. break;
  731. case PARAM_CENTER:
  732. g_value_set_string (value, "x-center");
  733. break;
  734. case PARAM_LAMINO:
  735. g_value_set_string (value, "lamino-angle");
  736. break;
  737. }
  738. break;
  739. default:
  740. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  741. break;
  742. }
  743. }
  744. static void
  745. ufo_anka_backproject_task_class_init (UfoAnkaBackprojectTaskClass *klass)
  746. {
  747. GObjectClass *oclass;
  748. UfoNodeClass *node_class;
  749. oclass = G_OBJECT_CLASS (klass);
  750. node_class = UFO_NODE_CLASS (klass);
  751. oclass->finalize = ufo_anka_backproject_task_finalize;
  752. oclass->set_property = ufo_anka_backproject_task_set_property;
  753. oclass->get_property = ufo_anka_backproject_task_get_property;
  754. GParamSpec *region_vals = g_param_spec_int ("region_values",
  755. "Region values",
  756. "Elements in regions",
  757. G_MININT,
  758. G_MAXINT,
  759. (gint) 0,
  760. G_PARAM_READWRITE);
  761. GParamSpec *float_region_vals = g_param_spec_float ("float_region_values",
  762. "Float Region values",
  763. "Elements in float regions",
  764. -G_MAXFLOAT,
  765. G_MAXFLOAT,
  766. 0.0f,
  767. G_PARAM_READWRITE);
  768. properties[PROP_X_REGION] =
  769. g_param_spec_value_array ("x-region",
  770. "X region for reconstruction as (from, to, step)",
  771. "X region for reconstruction as (from, to, step)",
  772. region_vals,
  773. G_PARAM_READWRITE);
  774. properties[PROP_Y_REGION] =
  775. g_param_spec_value_array ("y-region",
  776. "Y region for reconstruction as (from, to, step)",
  777. "Y region for reconstruction as (from, to, step)",
  778. region_vals,
  779. G_PARAM_READWRITE);
  780. properties[PROP_Z] =
  781. g_param_spec_float ("z",
  782. "Z coordinate of the reconstructed slice",
  783. "Z coordinate of the reconstructed slice",
  784. -G_MAXFLOAT,
  785. G_MAXFLOAT,
  786. 0.0f,
  787. G_PARAM_READWRITE);
  788. properties[PROP_REGION] =
  789. g_param_spec_value_array ("region",
  790. "Region for the parameter along z-axis as (from, to, step)",
  791. "Region for the parameter along z-axis as (from, to, step)",
  792. float_region_vals,
  793. G_PARAM_READWRITE);
  794. properties[PROP_PROJECTION_OFFSET] =
  795. g_param_spec_value_array ("projection-offset",
  796. "Offset to projection data as (x, y)",
  797. "Offset to projection data as (x, y) for the case input data \
  798. is cropped to the necessary range of interest",
  799. region_vals,
  800. G_PARAM_READWRITE);
  801. properties[PROP_CENTER] =
  802. g_param_spec_value_array ("center",
  803. "Center of the volume with respect to projections (x, y)",
  804. "Center of the volume with respect to projections (x, y), (rotation axes)",
  805. float_region_vals,
  806. G_PARAM_READWRITE);
  807. properties[PROP_OVERALL_ANGLE] =
  808. g_param_spec_float ("overall-angle",
  809. "Angle covered by all projections",
  810. "Angle covered by all projections (can be negative for negative steps "
  811. "in case only num-projections is specified",
  812. -G_MAXFLOAT,
  813. G_MAXFLOAT,
  814. G_PI,
  815. G_PARAM_READWRITE);
  816. properties[PROP_NUM_PROJECTIONS] =
  817. g_param_spec_uint ("num-projections",
  818. "Number of projections",
  819. "Number of projections",
  820. 0,
  821. 16384,
  822. 0,
  823. G_PARAM_READWRITE);
  824. properties[PROP_TOMO_ANGLE] =
  825. g_param_spec_float ("tomo-angle",
  826. "Tomographic rotation angle in radians",
  827. "Tomographic rotation angle in radians (used for acquiring projections)",
  828. -G_MAXFLOAT,
  829. G_MAXFLOAT,
  830. 0.0f,
  831. G_PARAM_READWRITE);
  832. properties[PROP_LAMINO_ANGLE] =
  833. g_param_spec_float ("lamino-angle",
  834. "Absolute laminogrpahic angle in radians",
  835. "Absolute laminogrpahic angle in radians determining the sample tilt",
  836. 0.0f,
  837. (float) G_PI / 2,
  838. 0.0f,
  839. G_PARAM_READWRITE);
  840. properties[PROP_PARAMETER] =
  841. g_param_spec_string ("parameter",
  842. "Which paramter will be varied along the z-axis",
  843. "Which paramter will be varied along the z-axis, from \"z\", \"x-center\", \"lamino-angle\"",
  844. "z",
  845. G_PARAM_READWRITE);
  846. for (guint i = PROP_0 + 1; i < N_PROPERTIES; i++)
  847. g_object_class_install_property (oclass, i, properties[i]);
  848. node_class->equal = ufo_anka_backproject_task_equal_real;
  849. g_type_class_add_private (klass, sizeof(UfoAnkaBackprojectTaskPrivate));
  850. }
  851. static void
  852. ufo_anka_backproject_task_init(UfoAnkaBackprojectTask *self)
  853. {
  854. UfoAnkaBackprojectTaskPrivate *priv;
  855. self->priv = priv = UFO_ANKA_BACKPROJECT_TASK_GET_PRIVATE(self);
  856. guint i;
  857. GValue int_zero = G_VALUE_INIT;
  858. GValue float_zero = G_VALUE_INIT;
  859. g_value_init (&int_zero, G_TYPE_INT);
  860. g_value_init (&float_zero, G_TYPE_FLOAT);
  861. g_value_set_int (&int_zero, 0);
  862. g_value_set_float (&float_zero, 0.0f);
  863. self->priv->x_region = g_value_array_new (3);
  864. self->priv->y_region = g_value_array_new (3);
  865. self->priv->region = g_value_array_new (3);
  866. self->priv->z = 0.0f;
  867. self->priv->projection_offset = g_value_array_new (2);
  868. self->priv->center = g_value_array_new (2);
  869. for (i = 0; i < 3; i++) {
  870. g_value_array_insert (self->priv->x_region, i, &int_zero);
  871. g_value_array_insert (self->priv->y_region, i, &int_zero);
  872. g_value_array_insert (self->priv->region, i, &float_zero);
  873. if (i < 2) {
  874. g_value_array_insert (self->priv->projection_offset, i, &int_zero);
  875. g_value_array_insert (self->priv->center, i, &float_zero);
  876. }
  877. }
  878. self->priv->num_projections = 0;
  879. self->priv->overall_angle = G_PI;
  880. self->priv->tomo_angle = -G_MAXFLOAT;
  881. self->priv->lamino_angle = 0.0f;
  882. self->priv->parameter = PARAM_Z;
  883. self->priv->count = 0;
  884. self->priv->generated = FALSE;
  885. }