Commit efe7b7ccb1b6ff21145e0165d561a6b233568f96

Authored by Pavel Govyadinov
1 parent 213d109c

Added a detailed spider class timer that is ran only in TIMING mode (has to be e…

…nabled in CMakeLists)
stim/cuda/cuda_texture.cuh
... ... @@ -99,8 +99,8 @@ namespace stim
99 99 &resource,
100 100 tex,
101 101 target,
102   -// cudaGraphicsMapFlagsReadOnly
103   - cudaGraphicsRegisterFlagsNone
  102 + cudaGraphicsMapFlagsReadOnly
  103 +// cudaGraphicsRegisterFlagsNone
104 104 )
105 105 );
106 106  
... ...
stim/cuda/spider_cost.cuh
... ... @@ -40,11 +40,10 @@ namespace stim{
40 40 ///Returns the value of the template pixel.
41 41 ///@param int x --location of a pixel.
42 42 __device__
43   - float Template(int x)
  43 + float Template(int x, int max_x)
44 44 {
45   - if(x < 32/6 || x > 32*5/6 || (x > 32*2/6 && x < 32*4/6)){
46   -// if(x < 2 || x > 13 || (x > 5 && x < 10)){
47   -// if(x < ceilf(16/6) || x > floorf(16*5/6) || (x > floorf(16*2/6) && x < ceilf(16*4/6))){
  45 + if(x < max_x/6 || x > max_x*5/6 || (x > max_x*2/6 && x < max_x*4/6))
  46 + {
48 47 return 1.0;
49 48 }else{
50 49 return 0.0;
... ... @@ -71,7 +70,7 @@ namespace stim{
71 70 int g_idx = blockIdx.y;
72 71  
73 72 float valIn = tex2D<unsigned char>(texIn, x, y)/255.0;
74   - float valTemp = Template(x);
  73 + float valTemp = Template(x, dx);
75 74  
76 75 // print[idx] = abs(valIn); ///temporary
77 76  
... ... @@ -149,7 +148,7 @@ namespace stim{
149 148 for( int i = 0; i<DIM_Y; i++){
150 149 if(output[i] < mini){
151 150 mini = output[i];
152   - idx = i;
  151 + idx = i;
153 152 }
154 153 }
155 154  
... ...
stim/gl/gl_spider.h
... ... @@ -31,6 +31,11 @@
31 31  
32 32 #include <iostream>
33 33 #include <fstream>
  34 +#ifdef TIMING
  35 + #include <ctime>
  36 + #include <cstdio>
  37 +#endif
  38 +
34 39 #ifdef TESTING
35 40 #include <iostream>
36 41 #include <cstdio>
... ... @@ -48,6 +53,16 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
48 53 //doesn't even need the texture iD really.
49 54 private:
50 55  
  56 + #ifdef TIMING
  57 + double branch_time = 0;
  58 + double direction_time = 0;
  59 + double position_time = 0;
  60 + double size_time = 0;
  61 + double cost_time = 0;
  62 + double network_time = 0;
  63 + double hit_time = 0;
  64 + #endif
  65 +
51 66 //
52 67 stim::vec3<float> p; //vector designating the position of the spider.
53 68 stim::vec3<float> d; //vector designating the orientation of the spider
... ... @@ -118,11 +133,19 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
118 133 void
119 134 findOptimalDirection()
120 135 {
  136 + #ifdef TIMING
  137 + gpuStartTimer();
  138 + #endif
121 139 setMatrix(); //create the transformation matrix.
122 140 glCallList(dList); //move the templates to p, d, m.
  141 + glFlush();
  142 + #ifdef TIMING
  143 + direction_time += gpuStopTimer();
  144 + #endif
123 145 #ifdef TESTING
124   - test(texbufferID, GL_TEXTURE_2D,2*t_length,numSamples*t_length, "Final_Cost_Direction.bmp");
  146 +// test(texbufferID, GL_TEXTURE_2D,2*t_length,numSamples*t_length, "Final_Cost_Direction.bmp");
125 147 #endif
  148 +
126 149 int best = getCost(texbufferID,numSamples); //find min cost.
127 150 stim::vec<float> next( //find next vector.
128 151 dV[best][0]*S[0]*R[0],
... ... @@ -143,10 +166,18 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
143 166 void
144 167 findOptimalPosition()
145 168 {
  169 + #ifdef TIMING
  170 + gpuStartTimer();
  171 + #endif
146 172 setMatrix(); //create the transformation matrix.
147 173 glCallList(dList+1); //move the templates to p, d, m.
  174 + glFlush();
  175 + #ifdef TIMING
  176 + position_time += gpuStopTimer();
  177 + #endif
  178 +
148 179 #ifdef TESTING
149   - test(ptexbufferID, GL_TEXTURE_2D,2*t_length, numSamplesPos*t_length, "Final_Cost_Position.bmp");
  180 +// test(ptexbufferID, GL_TEXTURE_2D,2*t_length, numSamplesPos*t_length, "Final_Cost_Position.bmp");
150 181 #endif
151 182 int best = getCost(ptexbufferID, numSamplesPos); //find min cost.
152 183 // std::cerr << best << std::endl;
... ... @@ -169,10 +200,17 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
169 200 void
170 201 findOptimalScale()
171 202 {
  203 + #ifdef TIMING
  204 + gpuStartTimer();
  205 + #endif
172 206 setMatrix(); //create the transformation.
173 207 glCallList(dList+2); //move the templates to p, d, m.
  208 + glFlush();
  209 + #ifdef TIMING
  210 + size_time += gpuStopTimer();
  211 + #endif
174 212 #ifdef TESTING
175   - test(mtexbufferID, GL_TEXTURE_2D, 2*t_length, numSamplesMag*t_length, "Final_Cost_Position.bmp");
  213 +// test(mtexbufferID, GL_TEXTURE_2D, 2*t_length, numSamplesMag*t_length, "Final_Cost_Position.bmp");
176 214 #endif
177 215 int best = getCost(mtexbufferID, numSamplesMag); //get best cost.
178 216 setMagnitude(m[0]*mV[best][0]); //adjust the magnitude.
... ... @@ -231,6 +269,10 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
231 269 void
232 270 branchDetection2(int n = 8, int l_template = 8, int l_square = 8)
233 271 {
  272 + #ifdef TIMING
  273 + gpuStartTimer();
  274 + #endif
  275 +
234 276 if(cL.size() < 4){}
235 277 else{
236 278 setMatrix(1);
... ... @@ -273,6 +315,9 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
273 315 }
274 316 }
275 317 }
  318 + #ifdef TIMING
  319 + branch_time += gpuStopTimer();
  320 + #endif
276 321 }
277 322  
278 323  
... ... @@ -305,58 +350,6 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
305 350 //---------------------TEMPLATE CREATION METHODS----------------------------//
306 351 //--------------------------------------------------------------------------//
307 352  
308   - void
309   - MonteCarloDirectionVectors(int nSamples, float solidAngle = 2*M_PI)
310   - {
311   -///equal area projection.
312   -// std::vector<stim::vec<float> > vecsUni;
313   -// for(int i = 0; i < nSamples; i++)
314   -// {
315   -// stim::vec<float> a = uniformRandVector();
316   -// a[1] = a[1]*2*M_PI;
317   -// a[0] = a[0]*(solidAngle);
318   -// a = a.cyl2cart();
319   -// stim::vec<float> b(
320   -// sqrt(1.-(pow(a[0],2)+(pow(a[1],2)))/4)*a[0],
321   -// sqrt(1.-(pow(a[0],2)+(pow(a[1],2)))/4)*a[1],
322   -// 1.-(pow(a[0],2)+pow(a[1],2))/2);
323   -// 2.*a[0]/(1.+pow(a[0],2)+pow(a[1],2)),
324   -// 2.*a[1]/(1.+pow(a[0],2)+pow(a[1],2)),
325   -// (-1.0+pow(a[0],2)+pow(a[1],2))/(1.0+pow(a[0],2)+pow(a[1],2)));
326   -// b = b.norm();
327   -// b[2] = -b[2]; //this doeswn't want to change
328   -// vecsUni.push_back(b);
329   -// }
330   - float PHI[2], Z[2], range;
331   - PHI[0] = asin(solidAngle/2);
332   - PHI[1] = asin(0);
333   -
334   - Z[0] = cos(PHI[0]);
335   - Z[1] = cos(PHI[1]);
336   -
337   -
338   - range = Z[0] - Z[1];
339   -
340   -
341   - float z, theta, phi;
342   -
343   - std::vector<stim::vec3<float> > vecsUni;
344   - for(int i = 0; i < numSamplesPos; i++)
345   - {
346   - stim::vec3<float> a(uniformRandom()*0.8, uniformRandom()*0.8, 0.0);
347   - a[0] = a[0]-0.4;
348   - a[1] = a[1]-0.4;
349   - vecsUni.push_back(a);
350   - }
351   -
352   - stringstream name;
353   - for(int i = 0; i < numSamplesPos; i++)
354   - name << vecsUni[i].str() << std::endl;
355   -
356   - std::ofstream outFile;
357   - outFile.open("New_Pos_Vectors.txt");
358   - outFile << name.str().c_str();
359   - }
360 353  
361 354  
362 355 ///@param solidAngle, the size of the arc to sample.
... ... @@ -759,9 +752,6 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
759 752 {
760 753 Bind();
761 754 CHECK_OPENGL_ERROR
762   - #ifdef TESTING
763   - start = std::clock();
764   - #endif
765 755 findOptimalDirection();
766 756 findOptimalPosition();
767 757 findOptimalScale();
... ... @@ -769,12 +759,6 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
769 759 Bind(btexbufferID, bfboID, 27);
770 760 branchDetection();
771 761 Unbind();
772   -
773   - #ifdef TESTING
774   - duration_sampling = duration_sampling +
775   - (std::clock() - start) / (double) CLOCKS_PER_SEC;
776   - num_sampling = num_sampling + 1.0;
777   - #endif
778 762 return current_cost;
779 763 }
780 764  
... ... @@ -792,17 +776,9 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
792 776 int
793 777 getCost()
794 778 {
795   - #ifdef TESTING
796   - start = std::clock();
797   - #endif
798 779 stim::vec<int> cost =
799 780 stim::cuda::get_cost(texbufferID, GL_TEXTURE_2D, numSamples);
800 781 cudaDeviceSynchronize();
801   - #ifdef TESTING
802   - duration_cuda = duration_cuda +
803   - (std::clock() - start) / (double) CLOCKS_PER_SEC;
804   - num_cuda = num_cuda + 1.0;
805   - #endif
806 782 current_cost = cost[1];
807 783 return cost[0];
808 784 }
... ... @@ -810,16 +786,13 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
810 786 int
811 787 getCost(GLuint tID, int n)
812 788 {
813   - #ifdef TESTING
814   - start = std::clock();
  789 + #ifdef TIMING
  790 + gpuStartTimer();
815 791 #endif
816 792 stim::vec<int> cost =
817 793 stim::cuda::get_cost(tID, GL_TEXTURE_2D, n, 2*t_length, t_length);
818   - cudaDeviceSynchronize();
819   - #ifdef TESTING
820   - duration_cuda = duration_cuda +
821   - (std::clock() - start) / (double) CLOCKS_PER_SEC;
822   - num_cuda = num_cuda + 1.0;
  794 + #ifdef TIMING
  795 + cost_time += gpuStopTimer();
823 796 #endif
824 797 current_cost = cost[1];
825 798 return cost[0];
... ... @@ -839,13 +812,9 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
839 812 //vectical rectangle forming the spider.
840 813 stim::rect<float> ver;
841 814  
842   - //Testing and Timing variables.
  815 + //Timing variables.
843 816 #ifdef TESTING
844 817 std::clock_t start;
845   - double duration_sampling = 0.0;
846   - double duration_cuda = 0.0;
847   - double num_sampling = 0.0;
848   - double num_cuda = 0.0;
849 818 #endif
850 819  
851 820 //--------------------------------------------------------------------------//
... ... @@ -939,10 +908,13 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
939 908 texID = id;
940 909 //GenerateFBO(16, numSamples*8);
941 910 GenerateFBO(t_length*2, numSamples*t_length, texbufferID, fboID);
  911 + std::cout << numSamples << std::endl;
942 912 CHECK_OPENGL_ERROR
943 913 GenerateFBO(t_length*2, numSamplesPos*t_length, ptexbufferID, pfboID);
  914 + std::cout << numSamplesPos << std::endl;
944 915 CHECK_OPENGL_ERROR
945 916 GenerateFBO(t_length*2, numSamplesMag*t_length, mtexbufferID, mfboID);
  917 + std::cout << numSamplesMag << std::endl;
946 918 CHECK_OPENGL_ERROR
947 919 GenerateFBO(16, 216, btexbufferID, bfboID);
948 920 CHECK_OPENGL_ERROR
... ... @@ -1204,6 +1176,47 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1204 1176 return seeds;
1205 1177 }
1206 1178  
  1179 + ///sets the number of direction templates.
  1180 + void
  1181 + setNumberDirectionTemplates(int n)
  1182 + {
  1183 + numSamples = n;
  1184 + }
  1185 +
  1186 + ///sets the number of position templates.
  1187 + void
  1188 + setNumberPositionTemplates(int n)
  1189 + {
  1190 + numSamplesPos = n;
  1191 + }
  1192 +
  1193 + ///sets the number of position templates.
  1194 + void
  1195 + setNumberSizeTemplates(int n)
  1196 + {
  1197 + numSamplesMag = n;
  1198 + }
  1199 +
  1200 + #ifdef TIMING
  1201 + ///Returns the timings at the moment the method is called.
  1202 + ///In the following order: Branch, Direction, Position, Size, Cost, Network, Hit_detetion.
  1203 + std::vector<double>
  1204 + getTimings()
  1205 + {
  1206 + std::vector <double> ret;
  1207 + ret.resize(7);
  1208 + ret[0] = branch_time;
  1209 + ret[1] = direction_time;
  1210 + ret[2] = position_time;
  1211 + ret[3] = size_time;
  1212 + ret[4] = cost_time;
  1213 + ret[5] = network_time;
  1214 + ret[6] = hit_time;
  1215 +
  1216 + return ret;
  1217 + }
  1218 + #endif
  1219 +
1207 1220 ///returns true if all seed stacks are empty, else false.
1208 1221 bool
1209 1222 Empty()
... ... @@ -1302,9 +1315,6 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1302 1315 {
1303 1316 Bind(texbufferID, fboID, numSamples, t_length);
1304 1317 CHECK_OPENGL_ERROR
1305   - #ifdef TESTING
1306   - start = std::clock();
1307   - #endif
1308 1318 findOptimalDirection();
1309 1319 Unbind();
1310 1320 Bind(ptexbufferID, pfboID, numSamplesPos, t_length);
... ... @@ -1315,12 +1325,6 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1315 1325 Unbind();
1316 1326 CHECK_OPENGL_ERROR
1317 1327  
1318   - #ifdef TESTING
1319   - duration_sampling = duration_sampling +
1320   - (std::clock() - start) / (double) CLOCKS_PER_SEC;
1321   - num_sampling = num_sampling + 1.0;
1322   - #endif
1323   - std::cout << current_cost << std::endl;
1324 1328 return current_cost;
1325 1329 }
1326 1330  
... ... @@ -1337,6 +1341,39 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1337 1341 //--------------------------------------------------------------------------//
1338 1342  
1339 1343 void
  1344 + MonteCarloDirectionVectors(int nSamples, float solidAngle = 2*M_PI)
  1345 + {
  1346 + float PHI[2], Z[2], range;
  1347 + PHI[0] = asin(solidAngle/2);
  1348 + PHI[1] = asin(0);
  1349 +
  1350 + Z[0] = cos(PHI[0]);
  1351 + Z[1] = cos(PHI[1]);
  1352 +
  1353 +
  1354 + range = Z[0] - Z[1];
  1355 +
  1356 +
  1357 + float z, theta, phi;
  1358 +
  1359 + std::vector<stim::vec3<float> > vecsUni;
  1360 + for(int i = 0; i < numSamplesPos; i++)
  1361 + {
  1362 + stim::vec3<float> a(uniformRandom()*0.8, uniformRandom()*0.8, 0.0);
  1363 + a[0] = a[0]-0.4;
  1364 + a[1] = a[1]-0.4;
  1365 + vecsUni.push_back(a);
  1366 + }
  1367 +
  1368 + stringstream name;
  1369 + for(int i = 0; i < numSamplesPos; i++)
  1370 + name << vecsUni[i].str() << std::endl;
  1371 +
  1372 + std::ofstream outFile;
  1373 + outFile.open("New_Pos_Vectors.txt");
  1374 + outFile << name.str().c_str();
  1375 + }
  1376 + void
1340 1377 DrawCylinder()
1341 1378 {
1342 1379 glNewList(dList+3, GL_COMPILE);
... ... @@ -1442,6 +1479,10 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1442 1479 selectObject(stim::vec3<float> loc, stim::vec3<float> dir, float mag)
1443 1480 {
1444 1481 //Define the varibles and turn on Selection Mode
  1482 +
  1483 + #ifdef TIMING
  1484 + gpuStartTimer();
  1485 + #endif
1445 1486  
1446 1487 float s = 3.0;
1447 1488 GLuint selectBuf[2048];
... ... @@ -1501,6 +1542,10 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1501 1542 // glEnable(GL_CULL_FACE);
1502 1543 hits = glRenderMode(GL_RENDER);
1503 1544 int found_hits = processHits(hits, selectBuf);
  1545 + #ifdef TIMING
  1546 + hit_time += gpuStopTimer();
  1547 + #endif
  1548 +
1504 1549 return found_hits;
1505 1550 }
1506 1551  
... ... @@ -1548,6 +1593,10 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1548 1593 addToNetwork(pair<stim::fiber<float>, int> in, stim::vec3<float> spos,
1549 1594 stim::vec<float> smag, stim::vec3<float> sdir)
1550 1595 {
  1596 + #ifdef TIMING
  1597 + double s = std::clock();
  1598 + #endif
  1599 +
1551 1600 std::vector<stim::vec3<float> > ce = in.first.centerline();
1552 1601 std::vector<stim::vec<float> > cm = in.first.centerlinemag();
1553 1602 //if the fiber is longer than 2 steps (the number it takes to diverge)
... ... @@ -1581,6 +1630,11 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1581 1630 } else { nt.addEdge(ce,cm, -1, -1);}
1582 1631 }
1583 1632 }
  1633 +
  1634 + #ifdef TIMING
  1635 + double network_time = (std::clock() - s) / (double) CLOCKS_PER_SEC;
  1636 + network_time += network_time * 1000.0;
  1637 + #endif
1584 1638 }
1585 1639  
1586 1640  
... ...
stim/image/image.h
... ... @@ -193,6 +193,7 @@ public:
193 193 get_interleaved_bgr(buffer);
194 194 cv::Mat cvImage((int)Y(), (int)X(), cv_type(), buffer);
195 195 cv::imwrite(filename, cvImage);
  196 + free(buffer);
196 197 }
197 198  
198 199 void set_interleaved(T* buffer, size_t width, size_t height, size_t channels){
... ...