Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cmath>
  4. #include <GL\freeglut.h>
  5. #include <amp.h>
  6. #include <amp_math.h>
  7. using namespace concurrency;
  8.  
  9.  
  10. const int N = 6000;
  11. int sw = 2;
  12.  
  13. std::vector <float> POSX(N);
  14. std::vector <float> POSY(N);
  15. std::vector <float> POSR(N + N);
  16. std::vector <float> AX_M(N);
  17. std::vector <float> AY_M(N);
  18.  
  19.  
  20. void initialization()
  21. {
  22.     glClearColor(0, 0, 0, 1);
  23.     glMatrixMode(GL_PROJECTION);
  24.     glLoadIdentity();
  25.     gluOrtho2D(-1000, 1000, -1000, 1000);
  26. }
  27.  
  28.  
  29. void display()
  30. {
  31.     glClear(GL_COLOR_BUFFER_BIT);
  32.     glPushMatrix();
  33.     glBegin(GL_POINTS);
  34.  
  35.     for (int draw = 0; draw < N; draw++) {
  36.         glColor3f(1, 1, 1);
  37.         glVertex2f(POSX[draw] += POSR[draw], POSY[draw] += POSR[draw + N]);
  38.     }
  39.  
  40.     glEnd();
  41.  
  42.     glPopMatrix();
  43.     glutSwapBuffers();
  44. }
  45.  
  46.  
  47. void Timer(int)
  48. {
  49.     if (sw != 1)
  50.     {
  51.     for (int first = 0; first < N; first++) {
  52.         for (int next = 0; next < N; next++) {
  53.             if (first != next) {
  54.                 AX_M[first] = 0.1 * (POSX[next] - POSX[first]) / sqrt((POSX[next] - POSX[first])*(POSX[next] - POSX[first]) + (POSY[next] - POSY[first])*(POSY[next] - POSY[first]));
  55.                 AY_M[first] = 0.1 * (POSY[next] - POSY[first]) / sqrt((POSX[next] - POSX[first])*(POSX[next] - POSX[first]) + (POSY[next] - POSY[first])*(POSY[next] - POSY[first]));
  56.             }
  57.         }
  58.         POSR[first] += AX_M[first];
  59.         POSR[first + N] += AY_M[first];
  60.         }
  61.     }
  62.  
  63.     else
  64.     {
  65.         array_view<float, 1> Matrix_X(N, POSX), Matrix_Y(N, POSY), Matrix_R(N + N, POSR), AX(N, AX_M), AY(N, AY_M);
  66.         Matrix_R.discard_data();
  67.         parallel_for_each(
  68.             Matrix_R.extent, [=](index<1> first) restrict(amp) {
  69.             for (int next = 0; next < N; next++) {
  70.                 if (first[0] != next) {
  71.                     AX[first[0]] = 0.1 * (Matrix_X[next] - Matrix_X[first[0]]) / fast_math::sqrt((Matrix_X[next] - Matrix_X[first[0]])*(Matrix_X[next] - Matrix_X[first[0]]) + (Matrix_Y[next] - Matrix_Y[first[0]])*(Matrix_Y[next] - Matrix_Y[first[0]]));
  72.                     AY[first[0]] = 0.1 * (Matrix_Y[next] - Matrix_Y[first[0]]) / fast_math::sqrt((Matrix_X[next] - Matrix_X[first[0]])*(Matrix_X[next] - Matrix_X[first[0]]) + (Matrix_Y[next] - Matrix_Y[first[0]])*(Matrix_Y[next] - Matrix_Y[first[0]]));
  73.                 }
  74.             }
  75.             Matrix_R[first[0]] += AX[first[0]];
  76.             Matrix_R[first[0] + N] += AY[first[0]];
  77.         });
  78.         Matrix_R.synchronize();
  79.                
  80.     }
  81.     display();
  82.     glutTimerFunc(0, Timer, 0);
  83. }
  84.  
  85.  
  86. void characters()
  87. {
  88.     for (int i = 0; i < N; i++) {
  89.         POSX[i] = rand() % 1000 - 500;
  90.         POSY[i] = rand() % 1000 - 500;
  91.  
  92.         for (int j = 0; j < i; j++) {
  93.             if ((i != j) && (POSX[i] == POSX[j]) && (POSY[i] == POSY[j]))
  94.                 i--;
  95.         }
  96.     }
  97. }
  98.  
  99.  
  100. void keyboard(unsigned char key, int x, int y)
  101. {
  102.     switch (key)
  103.     {
  104.     case '1': sw = 1;
  105.         break;
  106.     case '2': sw = 2;
  107.         break;
  108.     }
  109.  
  110. }
  111.  
  112.  
  113. int main(int argc, char **argv)
  114. {
  115.     srand(time(0));
  116.     setlocale(LC_ALL, "russian");
  117.  
  118.     characters();
  119.  
  120.     std::cout << "[1] -- Select GPU\n";
  121.     std::cout << "[2] -- Select CPU\n";
  122.  
  123.     glutInit(&argc, argv);
  124.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  125.     glutInitWindowPosition(100, 100);
  126.     glutInitWindowSize(1000, 1000);
  127.     glutCreateWindow("YOBA N-Body.Ver. 0.5a");
  128.     glutDisplayFunc(display);
  129.  
  130.     glutTimerFunc(0, Timer, 0);
  131.     glutKeyboardFunc(keyboard);
  132.     initialization();
  133.     glutMainLoop();
  134.  
  135.     system("pause");
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement