Guest User

Untitled

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