Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <cmath>
- #include <GL\freeglut.h>
- #include <omp.h>
- #include <amp.h>
- #include <amp_math.h>
- #include <string>
- using namespace concurrency;
- const int N = 10000;
- int button = 2;
- //for FPS
- float fps;
- int frames = 0;
- float time_f;
- float timebase = 0;
- char text[5+10] = "FPS: ";
- char res[5+10] = "FPS: ";
- char buff[10];
- float POSX[N];
- float POSY[N];
- float POSR[N + N];
- float AX_M[N];
- float AY_M[N];
- void initialization()
- {
- glClearColor(0, 0, 0, 1);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(-1000, 1000, -1000, 1000);
- }
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glPushMatrix();
- glBegin(GL_POINTS);
- for (int draw = 0; draw < N; draw++) {
- glColor3f(1, 1, 1);
- glVertex2f(POSX[draw] += POSR[draw], POSY[draw] += POSR[draw + N]);
- }
- glEnd();
- glPopMatrix();
- glutSwapBuffers();
- frames++;
- time_f = glutGet(GLUT_ELAPSED_TIME);
- if (time_f - timebase >= 1000)
- {
- fps = frames*1000.0 / (time_f - timebase);
- timebase = time_f;
- frames = 0;
- strcpy(text, res);
- glutSetWindowTitle(strcat(text, itoa(fps, buff, 10)));
- }
- }
- void OMP_Math()
- {
- float den, res_x, res_y;
- #pragma omp parallel for private(den, AX_M, AY_M)
- for (int first = 0; first < N; first++) {
- AX_M[first] = 0; AY_M[first] = 0;
- for (int next = 0; next < N; next++) {
- if ((first != next) && ((POSX[next] != POSX[first]) || (POSY[next] != POSY[first]))) {
- den = 100000 * sqrt((POSX[next] - POSX[first])*(POSX[next] - POSX[first]) + (POSY[next] - POSY[first])*(POSY[next] - POSY[first]));
- AX_M[first] += (POSX[next] - POSX[first]) / den;
- AY_M[first] += (POSY[next] - POSY[first]) / den;
- }
- }
- POSR[first] += AX_M[first];
- POSR[first+N] += AY_M[first];
- }
- }
- void APM_Math()
- {
- 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);
- Matrix_R.discard_data();
- parallel_for_each(
- Matrix_R.extent, [=](index<1> first) restrict(amp) {
- AX[first[0]] = 0; AY[first[0]] = 0;
- for (int next = 0; next < N; next++) {
- if ((first[0] != next) && ((Matrix_X[next] != Matrix_X[first[0]]) || (Matrix_Y[next] != Matrix_Y[first[0]]))) {
- int den;
- 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]]));
- AX[first[0]] += (Matrix_X[next] - Matrix_X[first[0]]) / den;
- AY[first[0]] += (Matrix_Y[next] - Matrix_Y[first[0]]) / den;
- }
- }
- Matrix_R[first[0]] += AX[first[0]];
- Matrix_R[first[0] + N] += AY[first[0]];
- });
- Matrix_R.synchronize();
- }
- void Timer(int)
- {
- if (button != 1)
- {
- OMP_Math();
- }
- else
- {
- APM_Math();
- }
- display();
- glutTimerFunc(0, Timer, 0);
- }
- void characters()
- {
- for (int i = 0; i < N; i++) {
- POSX[i] = rand() % 1000 - 500;
- POSY[i] = rand() % 1000 - 500;
- for (int j = 0; j < i; j++) {
- if ((i != j) && (POSX[i] == POSX[j]) && (POSY[i] == POSY[j]))
- i--;
- }
- }
- }
- void keyboard(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case '1': button = 1;
- break;
- case '2': button = 2;
- break;
- }
- }
- int main(int argc, char **argv)
- {
- srand(time(0));
- setlocale(LC_ALL, "russian");
- characters();
- std::cout << "[1] -- Select GPU\n";
- std::cout << "[2] -- Select CPU\n";
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(100, 100);
- glutInitWindowSize(1000, 1000);
- glutCreateWindow("");
- glutDisplayFunc(display);
- glutTimerFunc(0, Timer, 0);
- glutKeyboardFunc(keyboard);
- initialization();
- glutMainLoop();
- return 0;
- }
- /*
- N = 20000
- Core i7-3770K @4700 MHz - 2 fps
- GeForce GTX 1080 @2037MHz - 58 fps
- N = 10000
- Core i7-3770K @4700 MHz - 6 fps
- GeForce GTX 1080 @2037MHz - 177 fps
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement