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

Revision 4, 13.2 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        }       
406
407        MPI_Barrier(MPI_COMM_WORLD);
408        sageInf.swapBuffer();
409        rgbBuffer = (GLubyte *)sageInf.getBuffer();
410
411        glutSwapBuffers( );
412}
413
414void
415Visible(int state)
416{
417    if (state == GLUT_VISIBLE) {
418        if (moving)
419            glutIdleFunc(Animate);
420    } else {
421        if (moving)
422            glutIdleFunc(NULL);
423    }
424}
425
426void
427menuSelect(int value)
428{
429    switch (value)
430    {
431        case 1:
432            moving = GL_TRUE;
433            glutIdleFunc(Animate);
434            break;
435        case 2:
436            moving = GL_FALSE;;
437            glutIdleFunc(NULL);
438            break;
439        case 3:
440            exit(0);
441            break;
442    }
443}
444
445int
446main(int argc, char **argv)
447{
448        // MPI Initialization
449        MPI_Init(&argc, &argv);
450        MPI_Comm_rank(MPI_COMM_WORLD, &rank);
451        MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
452        MPI_Barrier(MPI_COMM_WORLD);
453       
454        int resX = 1024, resY = 1024;
455        if (argc > 2) {
456                resX = atoi(argv[1]);
457                resY = atoi(argv[2]);
458        }       
459
460        dimX = nprocs-1;
461        dimY = 1;
462       
463        if (argc > 4) {
464                dimX = atoi(argv[3]);
465                dimY = atoi(argv[4]);
466        }       
467       
468        char procname[MPI_MAX_PROCESSOR_NAME];
469        int lenproc;
470        MPI_Get_processor_name(procname, &lenproc);
471        fprintf(stderr, "Processor %2d is machine [%s]\n", rank, procname);
472
473        // control master
474        if (rank == 0) {
475                sailConfig scfg;
476                scfg.init("atlantis-mpi.conf");
477                scfg.setAppName("atlantis-mpi");
478                scfg.rank = rank;
479                scfg.nodeNum = nprocs-1;
480                scfg.master = true;
481                scfg.rendering = false;
482
483                sageInf.init(scfg);
484
485                std::cerr << "sail initialized " << std::endl;
486               
487                MPI_Barrier(MPI_COMM_WORLD);
488
489                std::cerr << "entering message loop" << std::endl;
490                while(1) {
491                        sageMessage msg;
492                        if (sageInf.checkMsg(msg, false) > 0) {
493                                 switch (msg.getCode()) {
494                                         case APP_QUIT : {
495                                                //sageInf.shutdown();
496                                                int err;
497                                                MPI_Abort(MPI_COMM_WORLD, err);
498                                                // finalize
499                                                MPI_Finalize();
500                                                exit(0);
501                                                break;
502                                        }
503                                 }     
504                        }
505                        MPI_Barrier(MPI_COMM_WORLD);
506                }
507        }
508        // rendering slaves
509        else {
510                Xidx = (rank-1)%dimX;
511                Yidx = (rank-1)/dimX;
512
513                printf("Index x=%d y=%d\n", Xidx, Yidx);
514
515                sageRect atlantisImageMap;
516                atlantisImageMap.left = ((float)Xidx) / ((float)dimX);
517                atlantisImageMap.right = ((float)Xidx+1.0) / ((float)dimX);
518                atlantisImageMap.bottom = ((float)Yidx) / ((float)dimY);
519                atlantisImageMap.top = ((float)Yidx+1.0) / ((float)dimY);;
520
521                sailConfig scfg;
522                scfg.init("atlantis-mpi.conf");
523                scfg.setAppName("atlantis-mpi");
524                scfg.rank = rank;
525                scfg.resX = resX;
526                scfg.resY = resY;
527                scfg.imageMap = atlantisImageMap;
528                scfg.pixFmt = PIXFMT_888;
529                scfg.rowOrd = BOTTOM_TO_TOP;
530                scfg.nodeNum = nprocs-1;
531                scfg.master = false;   
532                scfg.rendering = true;
533
534                sageInf.init(scfg);
535
536                std::cerr << "sail initialized " << std::endl;
537
538                //glutInitWindowSize(512, 192);
539                gettimeofday(&tv_start,0);
540
541                glutInitWindowSize(resX, resY);
542                glutInitWindowPosition(0,0);
543                glutInit(&argc, argv);
544                glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
545                glutCreateWindow("GLUT Atlantis Demo");
546                printf("sharks: %d\n",NUM_SHARKS);
547                Init();
548                glutDisplayFunc(Display);
549                glutReshapeFunc(Reshape);
550                glutKeyboardFunc(Key);
551                moving = GL_TRUE;
552                glutIdleFunc(Animate);
553                glutVisibilityFunc(Visible);
554                glutCreateMenu(menuSelect);
555                glutAddMenuEntry("Start motion", 1);
556                glutAddMenuEntry("Stop motion", 2);
557                glutAddMenuEntry("Quit", 3);
558                glutAttachMenu(GLUT_RIGHT_BUTTON);
559
560                //glutFullScreen();
561
562                t1 = getTime();
563
564                if (rgbBuffer)
565                 delete [] rgbBuffer;
566        rgbBuffer = (GLubyte *)sageInf.getBuffer();
567
568                MPI_Barrier(MPI_COMM_WORLD);
569                std::cerr << "entering rendering loop" << std::endl;
570
571                glutMainLoop();
572
573                  // finalize
574                MPI_Finalize();
575        }
576       
577        // exit
578        return EXIT_SUCCESS;
579}
Note: See TracBrowser for help on using the repository browser.