source: trunk/src/testing/app/atlantis-mpi/atlantis.c @ 4

Revision 4, 12.7 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

Line 
1
2/* Copyright (c) Mark J. Kilgard, 1994. */
3
4/**
5 * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
6 * ALL RIGHTS RESERVED
7 * Permission to use, copy, modify, and distribute this software for
8 * any purpose and without fee is hereby granted, provided that the above
9 * copyright notice appear in all copies and that both the copyright notice
10 * and this permission notice appear in supporting documentation, and that
11 * the name of Silicon Graphics, Inc. not be used in advertising
12 * or publicity pertaining to distribution of the software without specific,
13 * written prior permission.
14 *
15 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
16 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
18 * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
19 * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
20 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
21 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
22 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
23 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
24 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
26 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
27 *
28 * US Government Users Restricted Rights
29 * Use, duplication, or disclosure by the Government is subject to
30 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
31 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
32 * clause at DFARS 252.227-7013 and/or in similar or successor
33 * clauses in the FAR or the DOD or NASA FAR Supplement.
34 * Unpublished-- rights reserved under the copyright laws of the
35 * United States.  Contractor/manufacturer is Silicon Graphics,
36 * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
37 *
38 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
39 */
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <math.h>
44#include <assert.h>
45#if defined(WIN32)
46#define WIN32_LEAN_AND_MEAN
47#include <windows.h>
48#include <winsock.h>
49#include <sys\timeb.h>
50int gettimeofday(struct timeval *tp, void *tzp);
51#else
52#include <sys/time.h>
53#endif
54
55#if defined(__APPLE__)
56#include <GLUT/glut.h>
57#include <OpenGL/glu.h>
58#else
59#include <GL/glut.h>
60#include <GL/glu.h>
61#endif
62
63#include "atlantis.h"
64
65#include <mpi.h>
66int rank   = 0;  // node rank
67int nprocs = 0;  // number of nodes
68int dimX, dimY, Xidx, Yidx;
69
70// headers for SAGE
71#include "sail.h"
72#include "misc.h"
73int winWidth, winHeight;
74GLubyte *rgbBuffer = 0;
75sail sageInf; // sail object
76
77#if defined(WIN32)
78int
79gettimeofday(struct timeval *tp, void *tzp)
80{
81        struct _timeb t;
82
83    _ftime(&t);
84    tp->tv_sec = t.time;
85    tp->tv_usec = t.millitm * 1000;
86    return 0;
87}
88#endif
89
90fishRec sharks[NUM_SHARKS];
91fishRec momWhale;
92fishRec babyWhale;
93fishRec dolph;
94
95GLboolean moving;
96int screenshot = 0;
97float t1,t2;
98struct timeval tv_start;
99
100double getTime()
101{
102    struct timeval tv;
103                       
104    gettimeofday(&tv,0);
105    return (double)(tv.tv_sec - tv_start.tv_sec) + (double)(tv.tv_usec - tv_start.tv_usec) / 1000000.0;
106}
107/*
108float abs(float f)
109{
110    if (f >= 0.0f) return f;
111    else return -f;
112}
113*/
114void
115InitFishs(void)
116{
117    int i;
118
119    for (i = 0; i < NUM_SHARKS; i++) {
120        sharks[i].x = 15000.0 + rand() % 6000;
121        sharks[i].y = (rand() % 20000) - 10000;
122        sharks[i].z = (rand() % 6000) - 5000;
123        sharks[i].psi = rand() % 360 - 180.0;
124        sharks[i].v = 1.0;
125    }
126
127    dolph.x = 30000.0;
128    dolph.y = 0.0;
129    dolph.z = 6000.0;
130    dolph.psi = 90.0;
131    dolph.theta = 0.0;
132    dolph.v = 3.0;
133
134    momWhale.x = 70000.0;
135    momWhale.y = 0.0;
136    momWhale.z = 0.0;
137    momWhale.psi = 90.0;
138    momWhale.theta = 0.0;
139    momWhale.v = 3.0;
140
141    babyWhale.x = 60000.0;
142    babyWhale.y = -2000.0;
143    babyWhale.z = -2000.0;
144    babyWhale.psi = 90.0;
145    babyWhale.theta = 0.0;
146    babyWhale.v = 3.0;
147}
148
149void
150Init(void)
151{
152    static float ambient[] =
153    {0.1, 0.1, 0.1, 1.0};
154    static float diffuse[] =
155    {1.0, 1.0, 1.0, 1.0};
156    static float position[] =
157    {0.0, 1.0, 0.0, 0.0};
158    static float mat_shininess[] =
159    {90.0};
160    static float mat_specular[] =
161    {0.8, 0.8, 0.8, 1.0};
162    static float mat_diffuse[] =
163    {0.46, 0.66, 0.795, 1.0};
164    static float mat_ambient[] =
165    {0.0, 0.1, 0.2, 1.0};
166    static float lmodel_ambient[] =
167    {0.4, 0.4, 0.4, 1.0};
168    static float lmodel_localviewer[] =
169    {1.0};
170
171    glFrontFace(GL_CW);
172
173    glDepthFunc(GL_LEQUAL);
174    glEnable(GL_DEPTH_TEST);
175
176    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
177    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
178    glLightfv(GL_LIGHT0, GL_POSITION, position);
179    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
180    glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_localviewer);
181    glEnable(GL_LIGHTING);
182    glEnable(GL_LIGHT0);
183
184    glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
185    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
186    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
187    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
188
189    InitFishs();
190
191    glClearColor(0.0, 0.5, 0.9, 0.0);
192}
193
194void
195Reshape(int width, int height)
196{
197    winWidth = width;
198    winHeight = height;
199
200    glViewport(0, 0, width, height);
201
202    glMatrixMode(GL_PROJECTION);
203    glLoadIdentity();
204    gluPerspective(4.0, 2.0, 10000.0, 400000.0);
205    glMatrixMode(GL_MODELVIEW);
206        glLoadIdentity();
207
208}
209
210void
211Animate(void)
212{
213    int i;
214
215    for (i = 0; i < NUM_SHARKS; i++) {
216        SharkPilot(&sharks[i]);
217        SharkMiss(i);
218    }
219    WhalePilot(&dolph);
220    dolph.phi++;
221    glutPostRedisplay();
222    WhalePilot(&momWhale);
223    momWhale.phi++;
224    WhalePilot(&babyWhale);
225    babyWhale.phi++;
226}
227
228/* ARGSUSED1 */
229void
230Key(unsigned char key, int x, int y)
231{
232        (void) x;
233        (void) y;
234    switch (key)
235        {
236          case 27:           /* Esc will quit */
237        exit(1);
238        MPI_Finalize();
239        break;
240          case 's':
241                screenshot++;
242                glutPostRedisplay( );
243                break;
244          case ' ':          /* space will advance frame */
245        if (!moving) {
246            Animate();
247        }
248    }
249}
250
251
252void
253save_frame( void )
254{
255/*      GLint viewport[4];
256        int x, y, width, height;
257        void *pixels;
258        FILE *f;
259
260        glGetIntegerv( GL_VIEWPORT, viewport );
261
262        x = viewport[0];
263        y = viewport[1];
264        width = viewport[2];
265        height = viewport[3];
266
267        printf( "saving %dx%d screenshot\n", width, height );
268
269        pixels = malloc( width * height * 3 );
270        assert( pixels );
271        memset( pixels, 0, width * height * 3 );
272
273        glReadPixels( x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels );
274
275
276        f = fopen( "screenshot.ppm", "wb" );
277        fprintf( f, "P6\n%d %d\n255\n", width, height );
278        for ( y = height - 1; y >= 0; y-- )
279                fwrite( (char *) pixels + y * width * 3, width, 3, f );
280        fclose( f );
281
282        free( pixels );
283
284        printf( "done\n" );*/
285}
286
287void
288print_performance( void )
289{
290/*      static int first = 1;
291         = 0
292        static int start, num_frames;
293        int current;
294
295        if ( first )
296        {
297                start = glutGet( GLUT_ELAPSED_TIME );
298                num_frames = 0;
299                first = 0;
300        }
301
302        num_frames++;
303        current = glutGet( GLUT_ELAPSED_TIME );
304
305        if ( current - start > 1000 )
306        {
307                double elapsed = 1e-3 * (double) ( current - start );
308                double rate = (double) num_frames / elapsed;
309                printf( "%5.1f fps\n", rate );
310
311                num_frames = 0;
312                start = current;
313        }*/
314}
315
316void
317Display( void )
318{
319    float ambient[] = {0.1, 0.1, 0.1, 1.0};
320    float diffuse[] = {1.0, 1.0, 1.0, 1.0};
321    float position[] = {0.0, 1.0, 0.0, 0.0};
322    float mat_shininess[] = {90.0};
323    float mat_specular[] = {0.8, 0.8, 0.8, 1.0};
324    float mat_diffuse[] = {0.46, 0.66, 0.795, 1.0};
325    float mat_ambient[] = {0.0, 0.1, 0.2, 1.0};
326    float lmodel_ambient[] = {0.4, 0.4, 0.4, 1.0};
327    float lmodel_localviewer[] = {0.0};
328
329    int i;
330
331    glFrontFace(GL_CCW);
332
333    glDepthFunc(GL_LEQUAL);
334    glEnable(GL_DEPTH_TEST);
335    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
336    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
337    glLightfv(GL_LIGHT0, GL_POSITION, position);
338    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
339    glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_localviewer);
340    glEnable(GL_LIGHTING);
341    glEnable(GL_LIGHT0);
342    glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
343    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
344    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
345    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
346    glClearColor(0.0, 0.5, 0.9, 0.0);
347    glMatrixMode(GL_PROJECTION);
348    glLoadIdentity();
349
350
351    //gluPerspective(40.0, 2.0, 10000.0, 400000.0);
352
353    GLfloat fov = 40.0;
354    GLfloat aspect = 2.0;
355    GLfloat near = 10000.0;
356    GLfloat far = 400000.0;
357    GLfloat range = near*tan(M_PI * (fov/2) / 180.0);
358
359#if 0
360        // standard view
361    glFrustum(-range*aspect,range*aspect,-range,range,near,far);
362#else
363        // split view
364    GLfloat Xstep = 2.0*range*aspect / (float)dimX;
365         GLfloat Ystep = 2.0*range / (float)dimY;
366         
367    glFrustum(-range*aspect + Xidx*Xstep, -range*aspect + (Xidx+1)*Xstep,
368                        -range + Yidx*Ystep, -range + (Yidx+1)*Ystep, near, far);
369#endif
370
371    glMatrixMode(GL_MODELVIEW);
372
373        glLoadIdentity();
374
375        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
376
377        for (i = 0; i < NUM_SHARKS; i++)
378        {
379                glPushMatrix();
380                FishTransform(&sharks[i]);
381                DrawShark(&sharks[i]);
382                glPopMatrix();
383        }
384
385    glPushMatrix();
386    FishTransform(&dolph);
387    DrawDolphin(&dolph);
388    glPopMatrix();
389
390    glPushMatrix();
391    FishTransform(&momWhale);
392    DrawWhale(&momWhale);
393    glPopMatrix();
394
395    glPushMatrix();
396    FishTransform(&babyWhale);
397    glScalef(0.45, 0.45, 0.3);
398    DrawWhale(&babyWhale);
399    glPopMatrix();
400    glFinish();
401
402
403        if (winWidth > 0)       {
404                glReadPixels(0, 0, winWidth, winHeight, GL_RGB, GL_UNSIGNED_BYTE, rgbBuffer);   
405                MPI_Barrier(MPI_COMM_WORLD);
406                sageInf.swapBuffer();
407                rgbBuffer = (GLubyte *)sageInf.getBuffer();
408               
409                sageMessage msg;
410                if (sageInf.checkMsg(msg, false) > 0) {
411                         switch (msg.getCode()) {
412                                 case APP_QUIT : {
413                                        sageInf.shutdown();
414                                        int err;
415                                        MPI_Abort(MPI_COMM_WORLD, err);
416                                        // finalize
417                                        MPI_Finalize();
418                                        exit(0);
419                                        break;
420                                }
421                         }     
422                }
423        }
424
425        glutSwapBuffers( );
426}
427
428void
429Visible(int state)
430{
431    if (state == GLUT_VISIBLE) {
432        if (moving)
433            glutIdleFunc(Animate);
434    } else {
435        if (moving)
436            glutIdleFunc(NULL);
437    }
438}
439
440void
441menuSelect(int value)
442{
443    switch (value)
444    {
445        case 1:
446            moving = GL_TRUE;
447            glutIdleFunc(Animate);
448            break;
449        case 2:
450            moving = GL_FALSE;;
451            glutIdleFunc(NULL);
452            break;
453        case 3:
454            exit(0);
455            break;
456    }
457}
458
459int
460main(int argc, char **argv)
461{
462        // MPI Initialization
463        MPI_Init(&argc, &argv);
464        MPI_Comm_rank(MPI_COMM_WORLD, &rank);
465        MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
466        MPI_Barrier(MPI_COMM_WORLD);
467       
468        int appID;
469        if (argc < 2)
470                appID = 0;
471        else
472                appID = atoi(argv[1]);
473               
474        int resX = 1024, resY = 1024;
475        if (argc > 3) {
476                resX = atoi(argv[2]);
477                resY = atoi(argv[3]);
478        }       
479
480        dimX = nprocs;
481        dimY = 1;
482       
483        if (argc > 5) {
484                dimX = atoi(argv[4]);
485                dimY = atoi(argv[5]);
486        }       
487       
488        char procname[MPI_MAX_PROCESSOR_NAME];
489        int lenproc;
490        MPI_Get_processor_name(procname, &lenproc);
491        fprintf(stderr, "Processor %2d is machine [%s]\n", rank, procname);
492
493        Xidx = rank%dimX;
494        Yidx = rank/dimX;
495       
496        printf("Index x=%d y=%d\n", Xidx, Yidx);
497       
498        sageRect atlantisImageMap;
499        atlantisImageMap.left = ((float)Xidx) / ((float)dimX);
500        atlantisImageMap.right = ((float)Xidx+1.0) / ((float)dimX);
501        atlantisImageMap.bottom = ((float)Yidx) / ((float)dimY);
502        atlantisImageMap.top = ((float)Yidx+1.0) / ((float)dimY);;
503
504        sailConfig scfg;
505        scfg.init("atlantis-mpi.conf");
506        scfg.setAppName("atlantis-mpi");
507        scfg.rank = rank;
508        scfg.appID = appID;
509        scfg.resX = resX;
510        scfg.resY = resY;
511        scfg.imageMap = atlantisImageMap;
512        scfg.pixFmt = PIXFMT_888;
513        scfg.rowOrd = BOTTOM_TO_TOP;
514        scfg.nodeNum = nprocs;
515       
516        if (rank == 0)
517                scfg.master = true;
518        else
519                scfg.master = false;   
520
521        sageInf.init(scfg);
522
523        std::cerr << "sail initialized " << std::endl;
524
525        MPI_Barrier(MPI_COMM_WORLD);
526
527  //glutInitWindowSize(512, 192);
528        gettimeofday(&tv_start,0);
529
530        glutInitWindowSize(resX, resY);
531        glutInitWindowPosition(0,0);
532        glutInit(&argc, argv);
533        glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
534        glutCreateWindow("GLUT Atlantis Demo");
535        printf("sharks: %d\n",NUM_SHARKS);
536        Init();
537        glutDisplayFunc(Display);
538        glutReshapeFunc(Reshape);
539        glutKeyboardFunc(Key);
540        moving = GL_TRUE;
541        glutIdleFunc(Animate);
542        glutVisibilityFunc(Visible);
543        glutCreateMenu(menuSelect);
544        glutAddMenuEntry("Start motion", 1);
545        glutAddMenuEntry("Stop motion", 2);
546        glutAddMenuEntry("Quit", 3);
547        glutAttachMenu(GLUT_RIGHT_BUTTON);
548
549        //glutFullScreen();
550
551        t1 = getTime();
552
553        if (rgbBuffer)
554            delete [] rgbBuffer;
555   rgbBuffer = (GLubyte *)sageInf.getBuffer();
556   
557        glutMainLoop();
558
559          // finalize
560        MPI_Finalize();
561
562          // exit
563        return EXIT_SUCCESS;
564}
Note: See TracBrowser for help on using the repository browser.