ufo-anka-backproject-task.c 37 KB

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