Advertisement
Guest User

C++ AMP

a guest
Apr 10th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.84 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. #include <string>
  9.  
  10. using namespace concurrency;
  11.  
  12. const int N = 1024;
  13. int button = 1;
  14.  
  15. //for FPS
  16. float fps;
  17. int frames = 0;
  18. float time_f;
  19. float timebase = 0;
  20. char text[5+10] = "FPS: ";
  21. char res[5+10] = "FPS: ";
  22. char buff[10];
  23.  
  24. float POSX[N];
  25. float POSY[N];
  26. float POSR[N + N];
  27. float AX_M[N];
  28. float AY_M[N];
  29.  
  30.  
  31. void initialization()
  32. {
  33.     glClearColor(0, 0, 0, 1);
  34.     glMatrixMode(GL_PROJECTION);
  35.     glLoadIdentity();
  36.     gluOrtho2D(-1000, 1000, -1000, 1000);
  37. }
  38.  
  39.  
  40. void display()
  41. {
  42.     glClear(GL_COLOR_BUFFER_BIT);
  43.     glPushMatrix();
  44.     glBegin(GL_POINTS);
  45.  
  46.     for (int draw = 0; draw < N; draw++) {
  47.         glColor3f(1, 1, 1);
  48.         glVertex2f(POSX[draw] += POSR[draw], POSY[draw] += POSR[draw + N]);
  49.     }
  50.  
  51.     glEnd();
  52.  
  53.     glPopMatrix();
  54.     glutSwapBuffers();
  55.  
  56.     frames++;
  57.     time_f = glutGet(GLUT_ELAPSED_TIME);
  58.     if (time_f - timebase >= 1000)
  59.     {
  60.         fps = frames*1000.0 / (time_f - timebase);
  61.         timebase = time_f;
  62.         frames = 0;
  63.         strcpy(text, res);
  64.         glutSetWindowTitle(strcat(text, itoa(fps, buff, 10)));
  65.     }
  66. }
  67.  
  68.  
  69. void OMP_Math()
  70. {
  71.     float den, res_x, res_y;
  72. #pragma omp parallel for private(den, AX_M, AY_M)
  73.     for (int first = 0; first < N; first++) {
  74.         AX_M[first] = 0; AY_M[first] = 0;
  75.         for (int next = 0; next < N; next++) {
  76.             if ((first != next) && ((POSX[next] != POSX[first]) || (POSY[next] != POSY[first]))) {
  77.                 den = 100000 * sqrt((POSX[next] - POSX[first])*(POSX[next] - POSX[first]) + (POSY[next] - POSY[first])*(POSY[next] - POSY[first]));
  78.                 AX_M[first] += (POSX[next] - POSX[first]) / den;
  79.                 AY_M[first] += (POSY[next] - POSY[first]) / den;
  80.             }
  81.         }
  82.         POSR[first] += AX_M[first];
  83.         POSR[first+N] += AY_M[first];
  84.     }
  85. }
  86.  
  87. void APM_Math()
  88. {
  89.     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);
  90.     Matrix_R.discard_data();
  91.     parallel_for_each(
  92.         Matrix_R.extent, [=](index<1> first) restrict(amp) {
  93.         AX[first[0]] = 0; AY[first[0]] = 0;
  94.         for (int next = 0; next < N; next++) {
  95.             if ((first[0] != next) && ((Matrix_X[next] != Matrix_X[first[0]]) || (Matrix_Y[next] != Matrix_Y[first[0]]))) {
  96.                 int den;
  97.                 den = 100000 * 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]]));
  98.                 AX[first[0]] += (Matrix_X[next] - Matrix_X[first[0]]) / den;
  99.                 AY[first[0]] += (Matrix_Y[next] - Matrix_Y[first[0]]) / den;
  100.             }
  101.         }
  102.         Matrix_R[first[0]] += AX[first[0]];
  103.         Matrix_R[first[0] + N] += AY[first[0]];
  104.     });
  105.     Matrix_R.synchronize();
  106. }
  107.  
  108.  
  109. void Timer(int)
  110. {
  111.     if (button != 1)
  112.     {
  113.         OMP_Math();
  114.     }
  115.  
  116.     else
  117.     {
  118.         APM_Math();
  119.     }
  120.  
  121.     display();
  122.     glutTimerFunc(0, Timer, 0);
  123. }
  124.  
  125.  
  126. void characters()
  127. {
  128.     for (int i = 0; i < N; i++) {
  129.         POSX[i] = rand() % 1000 - 500;
  130.         POSY[i] = rand() % 1000 - 500;
  131.  
  132.         for (int j = 0; j < i; j++) {
  133.             if ((i != j) && (POSX[i] == POSX[j]) && (POSY[i] == POSY[j]))
  134.                 i--;
  135.         }
  136.     }
  137. }
  138.  
  139.  
  140. void keyboard(unsigned char key, int x, int y)
  141. {
  142.     switch (key)
  143.     {
  144.     case '1': button = 1;
  145.         break;
  146.     case '2': button = 2;
  147.         break;
  148.     }
  149. }
  150.  
  151.  
  152. int main(int argc, char **argv)
  153. {
  154.     srand(time(0));
  155.     setlocale(LC_ALL, "russian");
  156.  
  157.     characters();
  158.  
  159.     std::cout << "[1] -- Select GPU\n";
  160.     std::cout << "[2] -- Select CPU\n";
  161.  
  162.     glutInit(&argc, argv);
  163.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
  164.     glutInitWindowPosition(100, 100);
  165.     glutInitWindowSize(1000, 1000);
  166.     glutCreateWindow("");
  167.     glutDisplayFunc(display);
  168.  
  169.     glutTimerFunc(0, Timer, 0);
  170.     glutKeyboardFunc(keyboard);
  171.     initialization();
  172.     glutMainLoop();
  173.  
  174.     return 0;
  175. }
  176.  
  177.  
  178. /*
  179. N = 20000
  180. Core i7-3770K @4700 MHz - 2 fps
  181. GeForce GTX 1080 @2037MHz - 58 fps
  182.  
  183. N = 10000
  184. Core i7-3770K @4700 MHz - 7 fps
  185. GeForce GTX 1080 @2037MHz - 172 fps
  186. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement