Blame view

kernels/mior_model.cl 7.01 KB
89f70c1ec   glaville   import current mc...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
  /**
   * DATA STRUCTURES
   */
  
  typedef struct MM {
      float   x;
      float   y;
      int     carbone;
      int     dormance;
  } MM;
  
  typedef struct OM {
      float   x;
      float   y;
      int     carbone;
      int     lock;
  } OM;
  
  typedef struct MiorWorld {
      int     nbMM;
      int     nbOM;
      int     RA;
      float   RR;
      float   GR;
      float   K;
      int     width;
      int     minSize;
      int     CO2;
      int     lock;
  } MiorWorld;
  
  
  typedef struct RandomGen {
  	ulong a;
  	ulong b;
  	ulong c;
  } RandomGen;
  
  
  /**
   * CONSTANTS
   */
  
  #define OFFER_INVALID -1
  #define OFFER_UNINIT  -2
  
  /**
   * HELPER FUNCTIONS
   */
  
  // Compatibility macros for OpenCL <= 1.1
  
  #if __OPENCL_VERSION__ <= CL_VERSION_1_0
      #define atomic_cmpxchg(p, o, v) atom_cmpxchg(p, o, v)
  #endif
  
  // End of compatibility macros
  
  
  #define LOCK_P(lock, id) while (atomic_cmpxchg(&(lock), -1, id) != id) {}
  #define LOCK_V(lock, id) while (atomic_cmpxchg(&(lock), id, -1) != -1) {}
  
  // RNG
  
  void rng_init(RandomGen *r, long seed)
  {
      r->a = seed;
      r->b = 0;
      r->c = 362436;
  }
  
  unsigned long rng_rand(RandomGen *r)
  {
      const long old = r->b;
      r->b = r->a * 1103515245 + 12345;
      r->a = (~old ^ (r->b >> 3)) - r->c++;
      return r->b;
  }
  
  float rng_rand_01(RandomGen *r)
  {
      return (rng_rand(r) & 4294967295) / 4294967295.0f;
  }
  
  void shuffle_indexes(global int * array, int size, RandomGen *r)
  {   
      for (int i = size - 1; i >= 1; i--) {
  		const int j = rng_rand_01(r) * (i + 1);
  		
  		const int t = array[j];
  		array[j] = array[i];
  		array[i] = t;
  	}
  }
  
  void shuffle_help(global int * array, int size, RandomGen *r)
  {   
      for (int i = size - 1; i >= 1; i--) {
  		const int j = rng_rand_01(r) * (i + 1);
  		
  		const int t = array[j];
  		array[j] = array[i];
  		array[i] = t;
  	}
  }
  
  
  kernel void shuffle_test(global int * array, int size)
  {
  	RandomGen rng;
  	rng_init(&rng, 42L);
  	
  	for (int i = 0; i < size; i++) {
  		array[i] = i;
  	}
  	
  	shuffle_help(array, size, &rng);
  }
  	
  // Cleanup
  
  void topo_clean(global int * targets, int size)
  {
      for (int i = 0; i < size; i++) {
          if (targets[i] != OFFER_INVALID) {
              targets[i] = OFFER_UNINIT;
          }
      }
  }
  
  /**
   * KERNELS
   */
  
  kernel void topology(
      global MM *mmList,
      global OM *omList,
      global int *topo,
      global MiorWorld *world)
  {
      const int iMM = get_global_id(0);
      const int iOM = get_global_id(1);
      
  	//float dx = omList[iOM].x - mmList[iMM].x;
  	//float dy = omList[iOM].y - mmList[iMM].y;
  	//float distance = sqrt(dx * dx + dy * dy);
  	
      //if (distance <= world->RA) {
  	//    topo[iMM * world->nbOM + iOM] = OFFER_UNINIT;
  	//} else {
  	//    topo[iMM * world->nbOM + iOM] = OFFER_INVALID;
  	//}
  	
  	const float dx = omList[iOM].x - mmList[iMM].x;
  	const float dy = omList[iOM].y - mmList[iMM].y;
  	const float distance = hypot(dx, dy);
  	
  	if (distance <= world->RA) {
  	    topo[iMM * world->nbOM + iOM] = OFFER_UNINIT;
  	} else {
  	    topo[iMM * world->nbOM + iOM] = OFFER_INVALID;
  	}
  }
  
  int live_breath(
      int iMM,
      global MM * mmList,
      global OM * omList,
      global int * targets,
      global MiorWorld * world,
      global int *omIndexes)
  {
      global MM * currentMM = mmList + iMM;
      global OM * currentOM;
      
      const int totalNeed = currentMM->carbone * world->RR;
      int remainingNeed = totalNeed;
      const int nbOM = world->nbOM;
      int i = 0, iOM = 0;
      int offer, consum;
      
      while (i < nbOM && remainingNeed > 0) {
          iOM = omIndexes[i];
          
     		if (targets[iOM] != OFFER_INVALID) {
      		currentOM = omList + iOM;
              
              offer = world->K * currentOM->carbone;
              consum = min(offer, remainingNeed);
              remainingNeed -= consum;
      	}
      	
      	i++;
      }
      
      if (remainingNeed > 0) {
      	currentMM->dormance = 1;
      } else {
      	currentMM->dormance = 0;
      	remainingNeed = totalNeed;
    
  		i = 0;
  		while (i < nbOM && remainingNeed > 0) {
  		    iOM = omIndexes[i];
  		    
  		    if (targets[iOM] != OFFER_INVALID) {
  		        currentOM = omList + iOM;
  		         
  		        LOCK_P(currentOM->lock, iMM);
  		        
  		        if (targets[iOM] != OFFER_UNINIT) {
  		            offer = targets[iOM];
  		        } else {
  		            offer = world->K * currentOM->carbone;
  		        }
  		        
  		        consum = min(offer, remainingNeed);
  		        
  		        remainingNeed -= consum;
  		        currentOM->carbone -= consum;
  		        
  		        LOCK_V(currentOM->lock, iMM);
  		        
  		        LOCK_P(world->lock, iMM);
  		        world->CO2 += consum;
  		        LOCK_V(world->lock, iMM);
  		        
  		        targets[iOM] = offer - consum;
  		    }
  		    
  		    i++;
  		}
  	}
      
      return remainingNeed;
  }
  
  int live_grow(
      int iMM,
      global MM * mmList,
      global OM * omList,
      global int * targets,
      global MiorWorld * world,
      global int *omIndexes)
  {
      global MM * currentMM = mmList + iMM;
      global OM * currentOM;
      
      const int totalNeed = currentMM->carbone * world->GR;
      int remainingNeed = totalNeed;
      const int nbOM = world->nbOM;
      int i = 0, iOM = 0;
      int offer, consum;
      
      while (i < nbOM && remainingNeed > 0) {
          iOM = omIndexes[i];
          
          if (targets[iOM] != OFFER_INVALID) {
              currentOM = omList + iOM;
              
              LOCK_P(currentOM->lock, iMM);
              
              if (targets[iOM] != OFFER_UNINIT) {
                  offer = targets[iOM];
              } else {
                  offer = world->K * currentOM->carbone;
              }
              
              consum = min(offer, remainingNeed);
              
              remainingNeed -= consum;
              currentOM->carbone -= consum;
              
              LOCK_V(currentOM->lock, iMM);
              
              currentMM->carbone += consum;
          }
          
          i++;
      }
      
      return remainingNeed;
  }
  
  kernel void live(
      global MM *mmList,
      global OM *omList,
      global int *topo,
      global MiorWorld *world,
      global int *omIndexes,
      long   seed)
  {
      int iMM = get_global_id(0);
      global int * targets = topo + iMM * world->nbOM;
      global int * indexes = omIndexes + iMM * world->nbOM;
      RandomGen rng;
      
      for (int i = 0; i < world->nbOM; i++) {
          indexes[i] = i;
      }
      
      if (seed != 0) {
          rng_init(&rng, seed + iMM);
          shuffle_indexes(indexes, world->nbOM, &rng);
      }
      
      // BREATHING
      live_breath(iMM, mmList, omList, targets, world, indexes);
      
      if (mmList[iMM].dormance == 0) {
      
      	// GROWTH
      	live_grow(iMM, mmList, omList, targets, world, indexes);
      }
      
      // CLEANUP
      topo_clean(targets, world->nbOM);
  }
  
  kernel void autolive(
      global MM *mmList,
      global OM *omList,
      global int *topo,
      global MiorWorld *world,
      global int *omIndexes,
      long   seed)
  {
      LOCK_P(world->lock, get_global_id(0));
      int CO2 = world->CO2;
      LOCK_V(world->lock, get_global_id(0));
      
      int oldCO2 = -1;
      
      while (CO2 != oldCO2) {
          live(mmList, omList, topo, world, omIndexes, seed);
          
          oldCO2 = CO2;
          LOCK_P(world->lock, get_global_id(0));
          CO2 = world->CO2;
          LOCK_V(world->lock, get_global_id(0));
      }   
  }