Statistiques
| Révision :

root / TrouNoir / trou_noir_OpenACC.c @ 292

Historique | Voir | Annoter | Télécharger (10,79 ko)

1
/*
2
        Programme original realise en Fortran 77 en mars 1994
3
        pour les Travaux Pratiques de Modelisation Numerique
4
        DEA d'astrophysique et techniques spatiales de Meudon
5

6
                par Herve Aussel et Emmanuel Quemener
7

8
        Conversion en C par Emmanuel Quemener en aout 1997
9
        Modification par Emmanuel Quemener en aout 2019
10

11
        Licence CC BY-NC-SA Emmanuel QUEMENER <emmanuel.quemener@gmail.com>
12

13
        Remerciements a :
14
 
15
        - Herve Aussel pour sa procedure sur le spectre de corps noir
16
        - Didier Pelat pour l'aide lors de ce travail
17
        - Jean-Pierre Luminet pour son article de 1979
18
        - Le Numerical Recipies pour ses recettes de calcul
19
        - Luc Blanchet pour sa disponibilite lors de mes interrogations en RG
20

21
        Compilation sous gcc ( Compilateur GNU sous Linux ) :
22

23
        Version FP32 : gcc -O3 -fopenacc  -ffast-math -DFP32 -foffload=nvptx-none -foffload="-O3 -misa=sm_35 -lm" -o trou_noir_openacc_FP32 trou_noir_OpenACC.c -lm
24
        Version FP64 : gcc -O3 -fopenacc  -ffast-math -DFP64 -foffload=nvptx-none -foffload="-O3 -misa=sm_35 -lm" -o trou_noir_openacc_FP64 trou_noir_OpenACC.c -lm
25

26
*/ 
27

    
28
#include <stdio.h>
29
#include <math.h>
30
#include <stdlib.h>
31
#include <string.h>
32
#include <sys/time.h>
33
#include <time.h>
34

    
35
#define nbr 256 /* Nombre de colonnes du spectre */
36

    
37
#define PI 3.14159265359
38

    
39
#define TRACKPOINTS 2048
40

    
41
#if TYPE == FP64
42
#define MYFLOAT double
43
#else
44
#define MYFLOAT float
45
#endif
46

    
47
#pragma acc routine
48
MYFLOAT atanp(MYFLOAT x,MYFLOAT y)
49
{
50
  MYFLOAT angle;
51

    
52
  angle=atan2(y,x);
53

    
54
  if (angle<0)
55
    {
56
      angle+=2*PI;
57
    }
58

    
59
  return angle;
60
}
61

    
62
#pragma acc routine
63
MYFLOAT f(MYFLOAT v)
64
{
65
  return v;
66
}
67

    
68
#pragma acc routine
69
MYFLOAT g(MYFLOAT u,MYFLOAT m,MYFLOAT b)
70
{
71
  return (3.*m/b*pow(u,2)-u);
72
}
73

    
74
#pragma acc routine
75
void calcul(MYFLOAT *us,MYFLOAT *vs,MYFLOAT up,MYFLOAT vp,
76
            MYFLOAT h,MYFLOAT m,MYFLOAT b)
77
{
78
  MYFLOAT c[4],d[4];
79

    
80
  c[0]=h*f(vp);
81
  c[1]=h*f(vp+c[0]/2.);
82
  c[2]=h*f(vp+c[1]/2.);
83
  c[3]=h*f(vp+c[2]);
84
  d[0]=h*g(up,m,b);
85
  d[1]=h*g(up+d[0]/2.,m,b);
86
  d[2]=h*g(up+d[1]/2.,m,b);
87
  d[3]=h*g(up+d[2],m,b);
88

    
89
  *us=up+(c[0]+2.*c[1]+2.*c[2]+c[3])/6.;
90
  *vs=vp+(d[0]+2.*d[1]+2.*d[2]+d[3])/6.;
91
}
92

    
93
#pragma acc routine
94
void rungekutta(MYFLOAT *ps,MYFLOAT *us,MYFLOAT *vs,
95
                MYFLOAT pp,MYFLOAT up,MYFLOAT vp,
96
                MYFLOAT h,MYFLOAT m,MYFLOAT b)
97
{
98
  calcul(us,vs,up,vp,h,m,b);
99
  *ps=pp+h;
100
}
101

    
102
#pragma acc routine
103
MYFLOAT decalage_spectral(MYFLOAT r,MYFLOAT b,MYFLOAT phi,
104
                         MYFLOAT tho,MYFLOAT m)
105
{
106
  return (sqrt(1-3*m/r)/(1+sqrt(m/pow(r,3))*b*sin(tho)*sin(phi)));
107
}
108

    
109
#pragma acc routine
110
MYFLOAT spectre(MYFLOAT rf,MYFLOAT q,MYFLOAT b,MYFLOAT db,
111
             MYFLOAT h,MYFLOAT r,MYFLOAT m,MYFLOAT bss)
112
{
113
  MYFLOAT flx;
114

    
115
  flx=exp(q*log(r/m))*pow(rf,4)*b*db*h;
116
  return(flx);
117
}
118

    
119
#pragma acc routine
120
MYFLOAT spectre_cn(MYFLOAT rf,MYFLOAT b,MYFLOAT db,
121
                MYFLOAT h,MYFLOAT r,MYFLOAT m,MYFLOAT bss)
122
{
123
  
124
  MYFLOAT flx;
125
  MYFLOAT nu_rec,nu_em,qu,temp_em,flux_int;
126
  int fi,posfreq;
127

    
128
#define planck 6.62e-34
129
#define k 1.38e-23
130
#define c2 9.e16
131
#define temp 3.e7
132
#define m_point 1.
133

    
134
#define lplanck (log(6.62)-34.*log(10.))
135
#define lk (log(1.38)-23.*log(10.))
136
#define lc2 (log(9.)+16.*log(10.))
137
  
138
  MYFLOAT v=1.-3./r;
139

    
140
  qu=1./sqrt((1.-3./r)*r)*(sqrt(r)-sqrt(6.)+sqrt(3.)/2.*log((sqrt(r)+sqrt(3.))/(sqrt(r)-sqrt(3.))* 0.17157287525380988 ));
141

    
142
  temp_em=temp*sqrt(m)*exp(0.25*log(m_point)-0.75*log(r)-0.125*log(v)+0.25*log(fabs(qu)));
143

    
144
  flux_int=0.;
145
  flx=0.;
146

    
147
  for (fi=0;fi<nbr;fi++)
148
    {
149
      nu_em=bss*(MYFLOAT)fi/(MYFLOAT)nbr;
150
      nu_rec=nu_em*rf;
151
      posfreq=(int)(nu_rec*(MYFLOAT)nbr/bss);
152
      if ((posfreq>0)&&(posfreq<nbr))
153
          {
154
          flux_int=2.*planck/c2*pow(nu_em,3)/(exp(planck*nu_em/(k*temp_em))-1.)*exp(3.*log(rf))*b*db*h;
155
            flx+=flux_int;
156
          }
157
    }
158

    
159
  return((MYFLOAT)flx);
160
}
161

    
162
#pragma acc routine
163
void impact(MYFLOAT d,MYFLOAT phi,int dim,MYFLOAT r,MYFLOAT b,MYFLOAT tho,MYFLOAT m,
164
            MYFLOAT *zp,MYFLOAT *fp,
165
            MYFLOAT q,MYFLOAT db,
166
            MYFLOAT h,MYFLOAT bss,int raie)
167
{
168
  MYFLOAT xe,ye;
169
  int xi,yi;
170
  MYFLOAT flx,rf;
171
  xe=d*sin(phi);
172
  ye=-d*cos(phi);
173

    
174
  xi=(int)xe+dim/2;
175
  yi=(int)ye+dim/2;
176
  
177
  rf=decalage_spectral(r,b,phi,tho,m);
178

    
179
  if (raie==0)
180
    {
181
      bss=1.e19;
182
      flx=spectre_cn(rf,b,db,h,r,m,bss);
183
    }
184
  else
185
    {
186
      bss=2.;
187
      flx=spectre(rf,q,b,db,h,r,m,bss);
188
    }
189
  
190
  if (zp[xi+dim*yi]==0.)
191
    {
192
      zp[xi+dim*yi]=1./rf;
193
    }
194
  
195
  if (fp[xi+dim*yi]==0.)
196
    {
197
      fp[xi+dim*yi]=flx;
198
    }
199

    
200
}
201

    
202
void sauvegarde_pgm(char nom[24],unsigned int *image,int dim)
203
{
204
  FILE            *sortie;
205
  unsigned long   i,j;
206
  
207
  sortie=fopen(nom,"w");
208
  
209
  fprintf(sortie,"P5\n");
210
  fprintf(sortie,"%i %i\n",dim,dim);
211
  fprintf(sortie,"255\n");
212

    
213
  for (j=0;j<dim;j++) for (i=0;i<dim;i++)
214
    {
215
      fputc(image[i+dim*j],sortie);
216
    }
217

    
218
  fclose(sortie);
219
}
220

    
221
int main(int argc,char *argv[])
222
{
223

    
224
  MYFLOAT m,rs,ri,re,tho;
225
  int q;
226

    
227
  MYFLOAT bss,stp;
228
  int nmx,dim;
229
  MYFLOAT bmx;
230
  MYFLOAT *zp,*fp;
231
  unsigned int *izp,*ifp;
232
  MYFLOAT zmx,fmx;
233
  int zimx=0,zjmx=0,fimx=0,fjmx=0;
234
  int raie,fcl,zcl;
235
  struct timeval tv1,tv2;
236
  MYFLOAT elapsed,cputime,epoch;
237
  int mtv1,mtv2;
238
  unsigned int epoch1,epoch2;
239

    
240
  /* Variables used inside pragma need to be placed inside loop ! */
241

    
242
  /* MYFLOAT d,db,b,nh,h,up,vp,pp,us,vs,ps; */
243
  /* MYFLOAT phi,phd,php,nr,r; */
244
  /* int ni,ii,imx,tst; */
245
  /* MYFLOAT rp[TRACKPOINTS]; */
246

    
247
  if (argc==2)
248
    {
249
      if (strcmp(argv[1],"-aide")==0)
250
        {
251
          printf("\nSimulation d'un disque d'accretion autour d'un trou noir\n");
252
          printf("\nParametres a definir :\n\n");
253
          printf("  1) Dimension de l'Image\n");
254
          printf("  2) Masse relative du trou noir\n");
255
          printf("  3) Dimension du disque exterieur\n");
256
          printf("  4) Inclinaison par rapport au disque (en degres)\n");
257
          printf("  5) Rayonnement de disque MONOCHROMATIQUE ou CORPS_NOIR\n");
258
          printf("  6) Impression des images NEGATIVE ou POSITIVE\n"); 
259
          printf("  7) Nom de l'image des Flux\n");
260
          printf("  8) Nom de l'image des decalages spectraux\n");
261
          printf("\nSi aucun parametre defini, parametres par defaut :\n\n");
262
          printf("  1) Dimension de l'image : 1024 pixels de cote\n");
263
          printf("  2) Masse relative du trou noir : 1\n");
264
          printf("  3) Dimension du disque exterieur : 12 \n");
265
          printf("  4) Inclinaison par rapport au disque (en degres) : 10\n");
266
          printf("  5) Rayonnement de disque CORPS_NOIR\n");
267
          printf("  6) Impression des images NEGATIVE ou POSITIVE\n"); 
268
                 printf("  7) Nom de l'image des flux : flux.pgm\n");
269
          printf("  8) Nom de l'image des z : z.pgm\n");
270
        }
271
    }
272
  
273
  if ((argc==9)||(argc==7))
274
    {
275
      printf("# Utilisation les valeurs definies par l'utilisateur\n");
276
      
277
      dim=atoi(argv[1]);
278
      m=atof(argv[2]);
279
      re=atof(argv[3]);
280
      tho=PI/180.*(90-atof(argv[4]));
281
      
282
      rs=2.*m;
283
      ri=3.*rs;
284

    
285
      if (strcmp(argv[5],"CORPS_NOIR")==0)
286
        {
287
          raie=0;
288
        }
289
      else
290
        {
291
          raie=1;
292
        }
293

    
294
    }
295
  else
296
    {
297
      printf("# Utilisation les valeurs par defaut\n");
298
      
299
      dim=1024;
300
      m=1.;
301
      rs=2.*m;
302
      ri=3.*rs;
303
      re=12.;
304
      tho=PI/180.*80;
305
      // Corps noir
306
      raie=0;
307
    }
308

    
309
  if (raie==1)
310
    {
311
      bss=2.;
312
      q=-2;
313
    }
314
  else
315
    {
316
      bss=1.e19;
317
      q=-0.75;
318
    }
319

    
320
      printf("# Dimension de l'image : %i\n",dim);
321
      printf("# Masse : %f\n",m);
322
      printf("# Rayon singularite : %f\n",rs);
323
      printf("# Rayon interne : %f\n",ri);
324
      printf("# Rayon externe : %f\n",re);
325
      printf("# Inclinaison a la normale en radian : %f\n",tho);
326
  
327
  zp=(MYFLOAT*)calloc(dim*dim,sizeof(MYFLOAT));
328
  fp=(MYFLOAT*)calloc(dim*dim,sizeof(MYFLOAT));
329

    
330
  izp=(unsigned int*)calloc(dim*dim,sizeof(unsigned int));  
331
  ifp=(unsigned int*)calloc(dim*dim,sizeof(unsigned int));
332

    
333
  nmx=dim;
334
  stp=dim/(2.*nmx);
335
  bmx=1.25*re;
336

    
337
  // Set start timer
338
  gettimeofday(&tv1, NULL);
339
  mtv1=clock()*1000/CLOCKS_PER_SEC;
340
  epoch1=time(NULL);
341

    
342
#pragma acc data copy(zp[0:dim*dim],fp[0:dim*dim])
343
#pragma acc parallel loop
344
  for (int n=1;n<=nmx;n++)
345
    { 
346
      MYFLOAT d,db,b,nh,h,up,vp,pp,us,vs,ps;
347
      MYFLOAT phi,phd,php,nr,r;
348
      int ni,ii,imx,tst;
349
      MYFLOAT rp[TRACKPOINTS];
350

    
351
      h=4.*PI/(MYFLOAT)TRACKPOINTS;
352
      d=stp*(MYFLOAT)n;
353

    
354
      db=bmx/(MYFLOAT)nmx;
355
      b=db*(MYFLOAT)n;
356
      up=0.;
357
      vp=1.;
358
      
359
      pp=0.;
360
      nh=1;
361

    
362
      rungekutta(&ps,&us,&vs,pp,up,vp,h,m,b);
363
    
364
      rp[(int)nh]=fabs(b/us);
365

    
366
      do
367
        {
368
          nh++;
369
          pp=ps;
370
          up=us;
371
          vp=vs;
372
          rungekutta(&ps,&us,&vs,pp,up,vp,h,m,b);
373
          
374
          rp[(int)nh]=b/us;
375
          
376
        } while ((rp[(int)nh]>=rs)&&(rp[(int)nh]<=rp[1]));
377
      
378
      for (int i=nh+1;i<TRACKPOINTS;i++)
379
        {
380
          rp[i]=0.;
381
        }
382
      
383
      imx=(int)(8*d);
384

    
385
      for (int i=0;i<=imx;i++)
386
        {
387
          phi=2.*PI/(MYFLOAT)imx*(MYFLOAT)i;
388
          phd=atanp(cos(phi)*sin(tho),cos(tho));
389
          phd=fmod(phd,PI);
390
          ii=0;
391
          tst=0;
392
          
393
          do
394
            {
395
              php=phd+(MYFLOAT)ii*PI;
396
              nr=php/h;
397
              ni=(int)nr;
398
          
399
              if ((MYFLOAT)ni<nh)
400
                {
401
                  r=(rp[ni+1]-rp[ni])*(nr-ni*1.)+rp[ni];
402
                }
403
              else
404
                {
405
                  r=rp[ni];
406
                }
407
              
408
              if ((r<=re)&&(r>=ri))
409
                {
410
                  tst=1;
411
                  impact(d,phi,dim,r,b,tho,m,zp,fp,q,db,h,bss,raie);
412
                }
413
              
414
              ii++;
415
            } while ((ii<=2)&&(tst==0));
416
        }
417
    }
418

    
419
  // Set stop timer
420
  gettimeofday(&tv2, NULL);
421
  mtv2=clock()*1000/CLOCKS_PER_SEC;
422
  epoch2=time(NULL);
423
  
424
  elapsed=(MYFLOAT)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
425
                    (tv2.tv_usec-tv1.tv_usec))/1000000;
426
  cputime=(MYFLOAT)((mtv2-mtv1)/1000.);  
427
  epoch=(MYFLOAT)(epoch2-epoch1);  
428

    
429
  fmx=fp[0];
430
  zmx=zp[0];
431

    
432
  for (int i=0;i<dim;i++) for (int j=0;j<dim;j++)
433
    {
434
      if (fmx<fp[i+dim*j])
435
        {
436
          fimx=i;
437
          fjmx=j;
438
          fmx=fp[i+dim*j];
439
        }
440
      
441
      if (zmx<zp[i+dim*j])
442
        {
443
          zimx=i;
444
          zjmx=j;
445
          zmx=zp[i+dim*j];
446
        }
447
    }
448

    
449
  printf("\nElapsed Time : %lf",(double)elapsed);
450
  printf("\nCPU Time : %lf",(double)cputime);
451
  printf("\nEpoch Time : %lf",(double)epoch);
452
  printf("\nZ max @(%.6f,%.6f) : %.6f",
453
         (float)zimx/(float)dim-0.5,0.5-(float)zjmx/(float)dim,zmx);
454
  printf("\nFlux max @(%.6f,%.6f) : %.6f\n\n",
455
         (float)fimx/(float)dim-0.5,0.5-(float)fjmx/(float)dim,fmx);
456

    
457
  // If input parameters set without output files precised
458
  if (argc!=7) {
459
  
460
    for (int i=0;i<dim;i++)
461
      for (int j=0;j<dim;j++)
462
        {
463
          zcl=(int)(255/zmx*zp[i+dim*(dim-1-j)]);
464
          fcl=(int)(255/fmx*fp[i+dim*(dim-1-j)]);
465
          
466
          if (strcmp(argv[6],"NEGATIVE")==0)
467
            {
468
              if (zcl>255)
469
                {
470
                  izp[i+dim*j]=0;
471
                }
472
              else
473
                {
474
                  izp[i+dim*j]=255-zcl;
475
                }
476
              
477
              if (fcl>255)
478
                {
479
                  ifp[i+dim*j]=0;
480
                }
481
              else
482
                {
483
                  ifp[i+dim*j]=255-fcl;
484
                }
485
          
486
            }
487
          else
488
            {
489
              if (zcl>255)
490
                {
491
                  izp[i+dim*j]=255;
492
                }
493
              else
494
                {
495
                  izp[i+dim*j]=zcl;
496
                }
497
              
498
              if (fcl>255)
499
                {
500
                  ifp[i+dim*j]=255;
501
                }
502
              else
503
                {
504
                  ifp[i+dim*j]=fcl;
505
                }
506
              
507
            }
508
        
509
        }
510
    
511
    if (argc==9)
512
      {
513
          sauvegarde_pgm(argv[7],ifp,dim);
514
          sauvegarde_pgm(argv[8],izp,dim);
515
      }
516
    else
517
      {
518
        sauvegarde_pgm("z.pgm",izp,dim);
519
        sauvegarde_pgm("flux.pgm",ifp,dim);
520
      }
521
  }
522
  else
523
    {
524
      printf("No output file precised, useful for benchmarks...\n\n");
525
    }
526

    
527
  free(zp);
528
  free(fp);
529
  
530
  free(izp);
531
  free(ifp);
532

    
533
}
534

    
535