PandA-2024.02
tiny_fixed.c
Go to the documentation of this file.
1 
6 #define SHELL_MAX 101
7 #define NUMPHOTONS 16
8 
9 #include <stdio.h>
10 #include "fixedptc.c"
11 #include "tiny_fixed.h"
12 #ifdef _OPENMP
13 #include <omp.h>
14 #endif
15 
16 const fixedpt mu_a = 131072; // fixedpt_fromint(2); /* Absorption Coefficient in 1/cm !!non-zero!! */
17 const fixedpt mu_s = 1310720; //fixedpt_fromint(20); /* Reduced Scattering Coefficient in 1/cm */
18 const fixedpt microns_per_shell = 327680; //fixedpt_fromint(50); /* Thickness of spherical shells in microns */
19 //long i;
20 const long photons = NUMPHOTONS;
21 const fixedpt albedo = 59578;
22 const fixedpt shells_per_mfp = 595782;
24 
26 {
27  int i;
28  int i4_huge = 2147483647;
29  int k;
30  fixedpt r;
31  k = *seed / 127773;
32  // printf("k = %i \n", k);
33 
34  *seed = 16807 * ( *seed - k * 127773 ) - k * 2836;
35  //printf("*seed = %i \n", *seed);
36 
37  if ( *seed < 0 )
38  {
39  *seed = *seed + i4_huge;
40  }
41 
42  r = ((unsigned)(*seed & 0xFFFF));
43 
44  return r;
45 }
46 
47 typedef struct
48 {
49  fixedpt x, y, z, u, v, w, weight;
50 
51 } photon;
52 
53 void processPhoton(int seed) {
54 
55  photon p;
56 
57  p.x = 0; p.y = 0; p.z = 0; /*launch*/
58  p.u = 0; p.v = 0; p.w = FIXEDPT_ONE;
59  p.weight = FIXEDPT_ONE;
60  fixedpt t, xi1, xi2;
61  long shell;
62 
63  for (;;) {
64  fixedpt r0 = get_uniform_fixed(&seed) + 1;
65  t = -fixedpt_ln(r0);
66  p.x += fixedpt_mul(t,p.u); //t * u;
67  p.y += fixedpt_mul(t,p.v); //t * v;
68  p.z += fixedpt_mul(t,p.w); //t * w;
69 
71  shell = jay >> FIXEDPT_FBITS;
72  // shell=(fixedpt_mul(fixedpt_sqrt(fixedpt_mul(x,x) + fixedpt_mul(y,y)+ fixedpt_mul(z,z)), shells_per_mfp)) >> FIXEDPT_FBITS; //sqrt(x*x+y*y+z*z)*shells_per_mfp; /*absorb*/
73  if (shell > SHELL_MAX-1) {
74  shell = SHELL_MAX-1;
75  }
76 #pragma omp atomic
77  heat[shell] += fixedpt_mul(FIXEDPT_ONE-albedo, p.weight); //(1.0-albedo)*weight;
79 
80  for(;;) {
81  fixedpt r1, r2;/*new direction*/
82  r1 = get_uniform_fixed(&seed);
83  r2 = get_uniform_fixed(&seed);
84  xi1 = (r1 << 1) - FIXEDPT_ONE;
85  xi2 = (r2 << 1) - FIXEDPT_ONE;
86  //if ((t=xi1*xi1+xi2*xi2)<=1) break;
87  t = fixedpt_mul(xi1,xi1) + fixedpt_mul(xi2,xi2);
88  if (t <= FIXEDPT_ONE)
89  break;
90  }
91  if (t == 0)
92  t = 1;
93  p.u = (t << 1) - FIXEDPT_ONE; //2.0 * t - 1.0;
95  p.v = fixedpt_mul(xi1, temp); // xi1 * sqrt((1-u*u)/t);
96  p.w = fixedpt_mul(xi2, temp); // xi2 * sqrt((1-u*u)/t);
97 
98  if (p.weight < 66){ // 66 = 0.001 in fixedpt /*roulette*/
99  if (get_uniform_fixed(&seed) > 6554) break; // 6554 = 0.1 in fixedpt
100  p.weight = fixedpt_div(p.weight, 6554); // /= 0.1;
101  }
102  }
103 }
104 
105 void process() {
106 #pragma omp parallel
107 {
108 #ifdef _OPENMP
109  const int n_threads = omp_get_num_threads();
110  const int max = photons / n_threads;
111  const int thread_id = omp_get_thread_num();
112  const int offset = thread_id * max;
113 #else
114  const int max = photons;
115  const int offset = 0;
116 #endif
117  int i;
118  for (i = 0; i < max; i++)
119  {
120  processPhoton(seeds[i+offset]);
121  }
122 }
123 }
124 
125 int main ()
126 {
127  int sum=0;
128  process();
129  for (int i=0;i<SHELL_MAX-1;i++) {
130  // fixedpt_print(heat[i]);
131  sum ^= heat[i]; // janders -- check correctness by XOR'ing all the values
132  }
133  printf("Result: %d\n", sum);
134  int res = 0;
135  if (sum == 56060)
136  {
137  printf("RESULT: PASS\n");
138  }
139  else
140  {
141  printf("RESULT: FAIL\n");
142  res = 1;
143  }
144  return res;
145 }
fixedpt x
Definition: tiny_fixed.c:49
fixedpt fixedpt_sqrt(fixedpt A)
Definition: fixedptc.c:96
#define FIXEDPT_FBITS
Definition: fixedptc.h:104
fixedpt w
Definition: tiny_fixed.c:49
#define NUMPHOTONS
Definition: tiny_fixed.c:7
const fixedpt albedo
Definition: tiny_fixed.c:21
const fixedpt shells_per_mfp
Definition: tiny_fixed.c:22
fixedpt fixedpt_ln(fixedpt x)
Definition: fixedptc.c:259
fixedpt y
Definition: tiny_fixed.c:49
#define max
Definition: backprop.h:17
int main()
Definition: tiny_fixed.c:125
int sum
Definition: dotproduct.h:3
static const uint32_t k[]
Definition: sha-256.c:22
void processPhoton(int seed)
Definition: tiny_fixed.c:53
const fixedpt microns_per_shell
Definition: tiny_fixed.c:18
fixedpt get_uniform_fixed(int *seed)
Definition: tiny_fixed.c:25
unsigned offset[NUM_VERTICES+1]
Definition: graph.h:3
fixedpt fixedpt_mul(fixedpt A, fixedpt B)
Definition: fixedptc.c:11
fixedpt fixedpt_div(fixedpt A, fixedpt B)
Definition: fixedptc.c:17
void process()
Definition: tiny_fixed.c:105
const long photons
Definition: tiny_fixed.c:20
fixedpt u
Definition: tiny_fixed.c:49
fixedpt v
Definition: tiny_fixed.c:49
const fixedpt mu_s
Definition: tiny_fixed.c:17
fixedpt heat[SHELL_MAX]
Definition: tiny_fixed.c:23
fixedpt z
Definition: tiny_fixed.c:49
x
Return the smallest n such that 2^n >= _x.
#define FIXEDPT_ONE
Definition: fixedptc.h:118
const fixedpt mu_a
Definition: tiny_fixed.c:16
int32_t fixedpt
Definition: fixedptc.h:79
#define SHELL_MAX
Tiny MCML benchmark.
Definition: tiny_fixed.c:6
fixedpt weight
Definition: tiny_fixed.c:49
int seeds[NUMPHOTONS]
Definition: tiny_fixed.h:1

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13