Skip to content

Commit 41c541e

Browse files
committed
Primary: Fixes to findroot_gpu function (sync from base findroot) to allow correct GPU execution.
Secondary changes: 1. calloc not available on gpu (in TRACE), use malloc 2. Don't use e_steps_low*e_steps_high but e_steps_low+e_steps_high for array allocation 3. Cosmetics to comp header
1 parent 28d582e commit 41c541e

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

mcstas-comps/samples/Phonon_simple.comp

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
* target_z: [m] position of target to focus at. Straight ahead.
5252
* target_index: [1] relative index of component to focus at, e.g. next is +1
5353
* gap: [meV] Bandgap energy (unphysical)
54-
* e_steps_low [50] Amount of possible intersections beneath the elastic line
55-
* e_steps_high [50] Amount of possible intersections above the elastic line
54+
* e_steps_low [1] Amount of possible intersections beneath the elastic line
55+
* e_steps_high [1] Amount of possible intersections above the elastic line
5656
*
5757
*
5858
* CALCULATED PARAMETERS:
@@ -307,7 +307,7 @@ double zridd_gpu(double x1, double x2, struct neutron_params *neutron, struct ph
307307
double root;
308308
// Energy gain and energy loss spaces are not equally big. We check uniformly
309309
// So we use two different ranges
310-
double range_low = brack_mid - brack_low;
310+
double range_low = brack_mid - brack_low;
311311
double range_high = brack_high - brack_mid;
312312
// First in energy loss for the neutron
313313
for (int i=0; i<phonon->e_steps_low_; i++){
@@ -335,25 +335,33 @@ double zridd_gpu(double x1, double x2, struct neutron_params *neutron, struct ph
335335
void findroots_gpu(double brack_low, double brack_mid, double brack_high,
336336
double *list, int* index, struct neutron_params *neutron, struct phonon_params *phonon)
337337
{
338-
double root,range=brack_mid-brack_low;
339-
int i, steps=100;
340-
341-
for (i=0; i<steps; i++)
342-
{
343-
root = zridd_gpu(brack_low+range*i/(int)steps,
344-
brack_low+range*(i+1)/(int)steps,
345-
neutron,phonon, ROOTACC);
338+
double root;
339+
// Energy gain and energy loss spaces are not equally big. We check uniformly
340+
// So we use two different ranges
341+
double range_low = brack_mid - brack_low;
342+
double range_high = brack_high - brack_mid;
343+
// First in energy loss for the neutron
344+
for (int i=0; i<phonon->e_steps_low_; i++){
345+
root = zridd_gpu(brack_low+range_low*i/ phonon->e_steps_low_,
346+
brack_low+range_low*(i+1)/ phonon->e_steps_low_,
347+
neutron, phonon, ROOTACC);
346348
if (root != UNUSED)
347349
{
348350
list[(*index)++]=root;
349351
}
350352
}
351-
root = zridd_gpu(brack_mid, brack_high, neutron,phonon, ROOTACC);
352-
if (root != UNUSED)
353-
{
353+
354+
// Then in energy gain for the neutron
355+
for (int i=0; i<phonon->e_steps_high_; i++){
356+
root = zridd_gpu(brack_low+range_high*i/ phonon->e_steps_high_,
357+
brack_low+range_high*(i+1)/ phonon->e_steps_high_,
358+
neutron, phonon, ROOTACC);
359+
if (root != UNUSED){
354360
list[(*index)++]=root;
355361
}
362+
}
356363
}
364+
357365

358366
#undef UNUSED
359367
#undef MAXRIDD
@@ -401,7 +409,22 @@ INITIALIZE
401409
%}
402410
TRACE
403411
%{
404-
double* vf_list = (double *)calloc( e_steps_low*e_steps_high, sizeof(double)); // List of allowed final velocities. Has length of scan_steps
412+
double* vf_list;
413+
#ifdef OPENACC
414+
vf_list = (double *)malloc( (e_steps_low + e_steps_high)* sizeof(double)); // List of allowed final velocities. Has length of scan_steps
415+
#else
416+
vf_list = (double *)calloc( e_steps_low + e_steps_high, sizeof(double)); // List of allowed final velocities. Has length of scan_steps
417+
#endif
418+
if (!vf_list) {
419+
printf("Memory allocation failed, fatal error!\n");
420+
exit(-1);
421+
}
422+
#ifdef OPENACC
423+
for (int ii=0; ii<e_steps_low+e_steps_high; ii++) {
424+
vf_list[ii]=0;
425+
}
426+
#endif
427+
405428
struct neutron_params neutron;
406429
double t0, t1; /* Entry/exit time for cylinder */
407430
double v_i, v_f; /* Neutron velocities: initial, final */

0 commit comments

Comments
 (0)