Guest User

Untitled

a guest
Oct 5th, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cmath>
  4. #include <GL\freeglut.h>
  5. #include <omp.h>
  6. #include <amp.h>
  7. #include <amp_math.h>
  8. using namespace concurrency;
  9.  
  10. const int N = 15000;
  11. int button = 2;
  12.  
  13. float POSX[N];
  14. float POSY[N];
  15. float POSR[N + N];
  16. float AX_M[N];
  17. 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 OMP_Math()
  48. {
  49.     int den;
  50. #pragma omp parallel for private(den, AX_M, AY_M)
  51.     for (int first = 0; first < N; first++) {
  52.         for (int next = 0; next < N; next++) {
  53.             if ((first != next) && ((POSX[next] != POSX[first]) || (POSY[next] != POSY[first]))) {
  54.                 den = 10 * sqrt((POSX[next] - POSX[first])*(POSX[next] - POSX[first]) + (POSY[next] - POSY[first])*(POSY[next] - POSY[first]));
  55.                 AX_M[first] = (POSX[next] - POSX[first]) / den;
  56.                 AY_M[first] = (POSY[next] - POSY[first]) / den;
  57.             }
  58.         }
  59.         POSR[first] += AX_M[first];
  60.         POSR[first + N] += AY_M[first];
  61.     }
  62. }
  63.  
  64. void APM_Math()
  65. {
  66.     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);
  67.     Matrix_R.discard_data();
  68.     parallel_for_each(
  69.         Matrix_R.extent, [=](index<1> first) restrict(amp) {
  70.         for (int next = 0; next < N; next++) {
  71.             if ((first[0] != next) && ((Matrix_X[next] != Matrix_X[first[0]]) || (Matrix_Y[next] != Matrix_Y[first[0]]))) {
  72.                 int den;
  73.                 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]]));
  74.                 AX[first[0]] = (Matrix_X[next] - Matrix_X[first[0]]) / den;
  75.                 AY[first[0]] = (Matrix_Y[next] - Matrix_Y[first[0]]) / den;
  76.             }
  77.         }
  78.         Matrix_R[first[0]] += AX[first[0]];
  79.         Matrix_R[first[0] + N] += AY[first[0]];
  80.     });
  81.     Matrix_R.synchronize();
  82. }
  83.  
  84. void Timer(int)
  85. {
  86.     if (button != 1)
  87.     {
  88.         OMP_Math();
  89.     }
  90.  
  91.     else
  92.     {
  93.         APM_Math();
  94.     }
  95.  
  96.     display();
  97.     glutTimerFunc(0, Timer, 0);
  98. }
  99.  
  100.  
  101. void characters()
  102. {
  103.     for (int i = 0; i < N; i++) {
  104.         POSX[i] = rand() % 1000 - 500;
  105.         POSY[i] = rand() % 1000 - 500;
  106.  
  107.         for (int j = 0; j < i; j++) {
  108.             if ((i != j) && (POSX[i] == POSX[j]) && (POSY[i] == POSY[j]))
  109.                 i--;
  110.         }
  111.     }
  112. }
  113.  
  114.  
  115. void keyboard(unsigned char key, int x, int y)
  116. {
  117.     switch (key)
  118.     {
  119.     case '1': button = 1;
  120.         break;
  121.     case '2': button = 2;
  122.         break;
  123.     }
  124.  
  125. }
  126.  
  127.  
  128. int main(int argc, char **argv)
  129. {
  130.     srand(time(0));
  131.     setlocale(LC_ALL, "russian");
  132.  
  133.     characters();
  134.  
  135.     std::cout << "[1] -- Select GPU\n";
  136.     std::cout << "[2] -- Select CPU\n";
  137.  
  138.     glutInit(&argc, argv);
  139.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  140.     glutInitWindowPosition(100, 100);
  141.     glutInitWindowSize(1000, 1000);
  142.     glutCreateWindow("YOBA N-Body.Ver. 0.7a");
  143.     glutDisplayFunc(display);
  144.  
  145.     glutTimerFunc(0, Timer, 0);
  146.     glutKeyboardFunc(keyboard);
  147.     initialization();
  148.     glutMainLoop();
  149.  
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment