6d2a14cc6f84964c185e6c7291e1851f819dfc9c
[GPU.git] / AnimateurFreeGL.cpp
1 #include <iostream>
2
3 #include "cudaTools.h"
4 #include "Device.h"
5 #include "MathTools.h"
6
7 #include "Chronos.h"
8
9 #include "AnimateurFreeGL.h"
10
11 using std::cout;
12 using std::endl;
13
14 /*----------------------------------------------------------------------*\
15 |* Declaration *|
16 \*---------------------------------------------------------------------*/
17
18 /*--------------------------------------*\
19 |* Imported *|
20 \*-------------------------------------*/
21
22 /*--------------------------------------*\
23 |* Public *|
24 \*-------------------------------------*/
25
26 /*--------------------------------------*\
27 |* Private *|
28 \*-------------------------------------*/
29
30 /*----------------------------------------------------------------------*\
31 |* Implementation *|
32 \*---------------------------------------------------------------------*/
33
34 /*--------------------------------------*\
35 |* Public *|
36 \*-------------------------------------*/
37
38 AnimateurFreeGL::AnimateurFreeGL(Animable_I* ptrAnimable, int nbIteration)
39 {
40 // Input
41 this->nbIteration = nbIteration;
42 this->ptrAnimable = ptrAnimable;
43
44 // Outputs
45 this->fps = -1;
46 this->timeElapseS = -1;
47
48 start();
49 printStat();
50 }
51
52 AnimateurFreeGL::~AnimateurFreeGL()
53 {
54 delete ptrAnimable; // discutable!
55 }
56
57 int AnimateurFreeGL::getFps(void)
58 {
59 return fps;
60 }
61
62 /*--------------------------------------*\
63 |* Private *|
64 \*-------------------------------------*/
65
66 int AnimateurFreeGL::start()
67 {
68 cout << "\n[AnimateurFreeGL] : started : " << ptrAnimable->getTitle() << endl;
69
70 int w = ptrAnimable->getW();
71 int h = ptrAnimable->getH();
72 size_t size = w * h * sizeof(uchar4);
73
74 uchar4* ptrDevImage;
75 HANDLE_ERROR(cudaMalloc((void**) &ptrDevImage, size));
76 HANDLE_ERROR(cudaMemset(ptrDevImage, 0, size));
77
78 int i = 0;
79 Chronos chrono;
80 const char* messageError=ptrAnimable->getTitle().c_str();
81 while (i < nbIteration)
82 {
83 ptrAnimable->runGPU(ptrDevImage);
84
85 Device::checkKernelError(messageError);
86 Device::synchronize(); // Important!
87
88 ptrAnimable->animationStep();
89
90 i++;
91 }
92
93 chrono.stop();
94 HANDLE_ERROR(cudaFree(ptrDevImage));
95
96 this->timeElapseS = chrono.getDeltaTime();
97 this->fps = nbIteration / timeElapseS;
98
99 return fps;
100 }
101
102 void AnimateurFreeGL::printStat()
103 {
104 cout << endl;
105 cout << "Benchmark : [" << ptrAnimable->getTitle()<<"]" << endl;
106 cout << "#Iteration : " << nbIteration << endl;
107 cout << "#secondes : " << timeElapseS << " (s)" << endl;
108 cout << "#fps : " << fps << endl;
109 }
110
111 /*----------------------------------------------------------------------*\
112 |* End *|
113 \*---------------------------------------------------------------------*/
114