Skip to content

Commit

Permalink
bringing back the standard clsim code for profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
fiedl committed May 26, 2018
1 parent 7d7414d commit 9968ac7
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 13 deletions.
127 changes: 127 additions & 0 deletions resources/kernels/lib/propagation_through_media/standard_clsim.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// This is the standard clsim algorithm for propagation through different media.
//
// Source:
// https://github.com/fiedl/clsim/blob/icesim-v05-00-07/resources/kernels/propagation_kernel.c.cl
//

#ifndef STANDARD_CLSIM_C
#define STANDARD_CLSIM_C

inline void apply_propagation_through_different_media_with_standard_clsim(
floating4_t photonPosAndTime, floating4_t photonDirAndWlen,
floating_t *sca_step_left, floating_t *abs_lens_left,
floating_t *distancePropagated, floating_t *distanceToAbsorption) {

// this block is along the lines of the PPC kernel
{
#ifdef getTiltZShift_IS_CONSTANT
#define effective_z (photonPosAndTime.z - getTiltZShift_IS_CONSTANT)
#else
// apply ice tilt
const floating_t effective_z =
photonPosAndTime.z - getTiltZShift(photonPosAndTime);
int currentPhotonLayer =
min(max(findLayerForGivenZPos(effective_z), 0), MEDIUM_LAYERS - 1);
#endif

const floating_t photon_dz = photonDirAndWlen.z;

// add a correction factor to the number of absorption lengths *abs_lens_left
// before the photon is absorbed. This factor will be taken out after this
// propagation step. Usually the factor is 1 and thus has no effect, but it
// is used in a direction-dependent way for our model of ice anisotropy.
const floating_t abs_len_correction_factor =
getDirectionalAbsLenCorrFactor(photonDirAndWlen);

*abs_lens_left *= abs_len_correction_factor;

// the "next" medium boundary (either top or bottom, depending on step
// direction)
floating_t mediumBoundary = (photon_dz < ZERO)
? (mediumLayerBoundary(currentPhotonLayer))
: (mediumLayerBoundary(currentPhotonLayer) +
(floating_t)MEDIUM_LAYER_THICKNESS);

// track this thing to the next scattering point
#ifdef PRINTF_ENABLED
// dbg_printf(" - next scatter in %f scattering lengths\n", *sca_step_left);
#endif

floating_t currentScaLen =
getScatteringLength(currentPhotonLayer, photonDirAndWlen.w);
floating_t currentAbsLen =
getAbsorptionLength(currentPhotonLayer, photonDirAndWlen.w);

floating_t ais =
(photon_dz * *sca_step_left -
my_divide((mediumBoundary - effective_z), currentScaLen)) *
(ONE / (floating_t)MEDIUM_LAYER_THICKNESS);
floating_t aia =
(photon_dz * *abs_lens_left -
my_divide((mediumBoundary - effective_z), currentAbsLen)) *
(ONE / (floating_t)MEDIUM_LAYER_THICKNESS);

#ifdef PRINTF_ENABLED
// dbg_printf(" - ais=%f, aia=%f, j_initial=%i\n", ais, aia,
// currentPhotonLayer);
#endif

// propagate through layers
int j = currentPhotonLayer;
if (photon_dz < 0) {
for (; (j > 0) && (ais < ZERO) && (aia < ZERO);
mediumBoundary -= (floating_t)MEDIUM_LAYER_THICKNESS,
currentScaLen = getScatteringLength(j, photonDirAndWlen.w),
currentAbsLen = getAbsorptionLength(j, photonDirAndWlen.w),
ais += my_recip(currentScaLen), aia += my_recip(currentAbsLen))
--j;
} else {
for (; (j < MEDIUM_LAYERS - 1) && (ais > ZERO) && (aia > ZERO);
mediumBoundary += (floating_t)MEDIUM_LAYER_THICKNESS,
currentScaLen = getScatteringLength(j, photonDirAndWlen.w),
currentAbsLen = getAbsorptionLength(j, photonDirAndWlen.w),
ais -= my_recip(currentScaLen), aia -= my_recip(currentAbsLen))
++j;
}

#ifdef PRINTF_ENABLED
// dbg_printf(" - j_final=%i\n", j);
#endif

if ((currentPhotonLayer == j) || ((my_fabs(photon_dz)) < EPSILON)) {
*distancePropagated = *sca_step_left * currentScaLen;
*distanceToAbsorption = *abs_lens_left * currentAbsLen;
} else {
const floating_t recip_photon_dz = my_recip(photon_dz);
*distancePropagated =
(ais * ((floating_t)MEDIUM_LAYER_THICKNESS) * currentScaLen +
mediumBoundary - effective_z) *
recip_photon_dz;
*distanceToAbsorption =
(aia * ((floating_t)MEDIUM_LAYER_THICKNESS) * currentAbsLen +
mediumBoundary - effective_z) *
recip_photon_dz;
}
#ifdef getTiltZShift_IS_CONSTANT
currentPhotonLayer = j;
#endif

#ifdef PRINTF_ENABLED
// dbg_printf(" - *distancePropagated=%f\n", *distancePropagated);
#endif

// get overburden for distance
if (*distanceToAbsorption < *distancePropagated) {
*distancePropagated = *distanceToAbsorption;
*abs_lens_left = ZERO;
} else {
*abs_lens_left =
my_divide(*distanceToAbsorption - *distancePropagated, currentAbsLen);
}

// hoist the correction factor back out of the absorption length
*abs_lens_left = my_divide(*abs_lens_left, abs_len_correction_factor);
}
}

#endif
39 changes: 26 additions & 13 deletions resources/kernels/propagation_kernel.c.cl
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,13 @@ inline void saveHit(
}


#ifdef DOUBLE_PRECISION
#define EPSILON 0.00000001
#else
#define EPSILON 0.00001f
#endif


// Profiling
// https://github.com/fiedl/hole-ice-study/issues/69
//
Expand All @@ -403,6 +410,7 @@ typedef unsigned long clock_t;

// `__CLSIM_DIR__` is replaced in `I3CLSimStepToPhotonConverterOpenCL::loadKernel`.
#include "__CLSIM_DIR__/resources/kernels/lib/propagation_through_media/propagation_through_media.c"
#include "__CLSIM_DIR__/resources/kernels/lib/propagation_through_media/standard_clsim.c"

__kernel void propKernel(
#ifndef TABULATE
Expand Down Expand Up @@ -500,12 +508,6 @@ __kernel void propKernel(
// step.numPhotons);
#endif

#ifdef DOUBLE_PRECISION
#define EPSILON 0.00000001
#else
#define EPSILON 0.00001f
#endif

uint photonsLeftToPropagate=step.numPhotons;
floating_t abs_lens_left=ZERO;
floating_t abs_lens_initial=ZERO;
Expand Down Expand Up @@ -619,20 +621,31 @@ __kernel void propKernel(
floating_t distancePropagated = 0;
floating_t distanceToAbsorption = 0;

apply_propagation_through_different_media(
// apply_propagation_through_different_media(
// photonPosAndTime,
// photonDirAndWlen,
// #ifdef HOLE_ICE
// numberOfCylinders,
// cylinderPositionsAndRadii,
// cylinderScatteringLengths,
// cylinderAbsorptionLengths,
// #endif
// &sca_step_left,
// &abs_lens_left,
// &distancePropagated,
// &distanceToAbsorption
// );

clock_t t1 = clock();
apply_propagation_through_different_media_with_standard_clsim(
photonPosAndTime,
photonDirAndWlen,
#ifdef HOLE_ICE
numberOfCylinders,
cylinderPositionsAndRadii,
cylinderScatteringLengths,
cylinderAbsorptionLengths,
#endif
&sca_step_left,
&abs_lens_left,
&distancePropagated,
&distanceToAbsorption
);
printf("PROFILING apply_propagation_through_different_media_with_standard_clsim %lu\n", clock() - t1);


#ifndef SAVE_ALL_PHOTONS
Expand Down

0 comments on commit 9968ac7

Please sign in to comment.