diff --git a/stim/cuda/cuda_texture.cuh b/stim/cuda/cuda_texture.cuh index a7ef60c..62327da 100644 --- a/stim/cuda/cuda_texture.cuh +++ b/stim/cuda/cuda_texture.cuh @@ -99,8 +99,8 @@ namespace stim &resource, tex, target, -// cudaGraphicsMapFlagsReadOnly - cudaGraphicsRegisterFlagsNone + cudaGraphicsMapFlagsReadOnly +// cudaGraphicsRegisterFlagsNone ) ); diff --git a/stim/cuda/spider_cost.cuh b/stim/cuda/spider_cost.cuh index 328e5ff..119cd82 100644 --- a/stim/cuda/spider_cost.cuh +++ b/stim/cuda/spider_cost.cuh @@ -40,11 +40,10 @@ namespace stim{ ///Returns the value of the template pixel. ///@param int x --location of a pixel. __device__ - float Template(int x) + float Template(int x, int max_x) { - if(x < 32/6 || x > 32*5/6 || (x > 32*2/6 && x < 32*4/6)){ -// if(x < 2 || x > 13 || (x > 5 && x < 10)){ -// if(x < ceilf(16/6) || x > floorf(16*5/6) || (x > floorf(16*2/6) && x < ceilf(16*4/6))){ + if(x < max_x/6 || x > max_x*5/6 || (x > max_x*2/6 && x < max_x*4/6)) + { return 1.0; }else{ return 0.0; @@ -71,7 +70,7 @@ namespace stim{ int g_idx = blockIdx.y; float valIn = tex2D(texIn, x, y)/255.0; - float valTemp = Template(x); + float valTemp = Template(x, dx); // print[idx] = abs(valIn); ///temporary @@ -149,7 +148,7 @@ namespace stim{ for( int i = 0; i #include +#ifdef TIMING + #include + #include +#endif + #ifdef TESTING #include #include @@ -48,6 +53,16 @@ class gl_spider : public virtual gl_texture //doesn't even need the texture iD really. private: + #ifdef TIMING + double branch_time = 0; + double direction_time = 0; + double position_time = 0; + double size_time = 0; + double cost_time = 0; + double network_time = 0; + double hit_time = 0; + #endif + // stim::vec3 p; //vector designating the position of the spider. stim::vec3 d; //vector designating the orientation of the spider @@ -118,11 +133,19 @@ class gl_spider : public virtual gl_texture void findOptimalDirection() { + #ifdef TIMING + gpuStartTimer(); + #endif setMatrix(); //create the transformation matrix. glCallList(dList); //move the templates to p, d, m. + glFlush(); + #ifdef TIMING + direction_time += gpuStopTimer(); + #endif #ifdef TESTING - test(texbufferID, GL_TEXTURE_2D,2*t_length,numSamples*t_length, "Final_Cost_Direction.bmp"); +// test(texbufferID, GL_TEXTURE_2D,2*t_length,numSamples*t_length, "Final_Cost_Direction.bmp"); #endif + int best = getCost(texbufferID,numSamples); //find min cost. stim::vec next( //find next vector. dV[best][0]*S[0]*R[0], @@ -143,10 +166,18 @@ class gl_spider : public virtual gl_texture void findOptimalPosition() { + #ifdef TIMING + gpuStartTimer(); + #endif setMatrix(); //create the transformation matrix. glCallList(dList+1); //move the templates to p, d, m. + glFlush(); + #ifdef TIMING + position_time += gpuStopTimer(); + #endif + #ifdef TESTING - test(ptexbufferID, GL_TEXTURE_2D,2*t_length, numSamplesPos*t_length, "Final_Cost_Position.bmp"); +// test(ptexbufferID, GL_TEXTURE_2D,2*t_length, numSamplesPos*t_length, "Final_Cost_Position.bmp"); #endif int best = getCost(ptexbufferID, numSamplesPos); //find min cost. // std::cerr << best << std::endl; @@ -169,10 +200,17 @@ class gl_spider : public virtual gl_texture void findOptimalScale() { + #ifdef TIMING + gpuStartTimer(); + #endif setMatrix(); //create the transformation. glCallList(dList+2); //move the templates to p, d, m. + glFlush(); + #ifdef TIMING + size_time += gpuStopTimer(); + #endif #ifdef TESTING - test(mtexbufferID, GL_TEXTURE_2D, 2*t_length, numSamplesMag*t_length, "Final_Cost_Position.bmp"); +// test(mtexbufferID, GL_TEXTURE_2D, 2*t_length, numSamplesMag*t_length, "Final_Cost_Position.bmp"); #endif int best = getCost(mtexbufferID, numSamplesMag); //get best cost. setMagnitude(m[0]*mV[best][0]); //adjust the magnitude. @@ -231,6 +269,10 @@ class gl_spider : public virtual gl_texture void branchDetection2(int n = 8, int l_template = 8, int l_square = 8) { + #ifdef TIMING + gpuStartTimer(); + #endif + if(cL.size() < 4){} else{ setMatrix(1); @@ -273,6 +315,9 @@ class gl_spider : public virtual gl_texture } } } + #ifdef TIMING + branch_time += gpuStopTimer(); + #endif } @@ -305,58 +350,6 @@ class gl_spider : public virtual gl_texture //---------------------TEMPLATE CREATION METHODS----------------------------// //--------------------------------------------------------------------------// - void - MonteCarloDirectionVectors(int nSamples, float solidAngle = 2*M_PI) - { -///equal area projection. -// std::vector > vecsUni; -// for(int i = 0; i < nSamples; i++) -// { -// stim::vec a = uniformRandVector(); -// a[1] = a[1]*2*M_PI; -// a[0] = a[0]*(solidAngle); -// a = a.cyl2cart(); -// stim::vec b( -// sqrt(1.-(pow(a[0],2)+(pow(a[1],2)))/4)*a[0], -// sqrt(1.-(pow(a[0],2)+(pow(a[1],2)))/4)*a[1], -// 1.-(pow(a[0],2)+pow(a[1],2))/2); -// 2.*a[0]/(1.+pow(a[0],2)+pow(a[1],2)), -// 2.*a[1]/(1.+pow(a[0],2)+pow(a[1],2)), -// (-1.0+pow(a[0],2)+pow(a[1],2))/(1.0+pow(a[0],2)+pow(a[1],2))); -// b = b.norm(); -// b[2] = -b[2]; //this doeswn't want to change -// vecsUni.push_back(b); -// } - float PHI[2], Z[2], range; - PHI[0] = asin(solidAngle/2); - PHI[1] = asin(0); - - Z[0] = cos(PHI[0]); - Z[1] = cos(PHI[1]); - - - range = Z[0] - Z[1]; - - - float z, theta, phi; - - std::vector > vecsUni; - for(int i = 0; i < numSamplesPos; i++) - { - stim::vec3 a(uniformRandom()*0.8, uniformRandom()*0.8, 0.0); - a[0] = a[0]-0.4; - a[1] = a[1]-0.4; - vecsUni.push_back(a); - } - - stringstream name; - for(int i = 0; i < numSamplesPos; i++) - name << vecsUni[i].str() << std::endl; - - std::ofstream outFile; - outFile.open("New_Pos_Vectors.txt"); - outFile << name.str().c_str(); - } ///@param solidAngle, the size of the arc to sample. @@ -759,9 +752,6 @@ class gl_spider : public virtual gl_texture { Bind(); CHECK_OPENGL_ERROR - #ifdef TESTING - start = std::clock(); - #endif findOptimalDirection(); findOptimalPosition(); findOptimalScale(); @@ -769,12 +759,6 @@ class gl_spider : public virtual gl_texture Bind(btexbufferID, bfboID, 27); branchDetection(); Unbind(); - - #ifdef TESTING - duration_sampling = duration_sampling + - (std::clock() - start) / (double) CLOCKS_PER_SEC; - num_sampling = num_sampling + 1.0; - #endif return current_cost; } @@ -792,17 +776,9 @@ class gl_spider : public virtual gl_texture int getCost() { - #ifdef TESTING - start = std::clock(); - #endif stim::vec cost = stim::cuda::get_cost(texbufferID, GL_TEXTURE_2D, numSamples); cudaDeviceSynchronize(); - #ifdef TESTING - duration_cuda = duration_cuda + - (std::clock() - start) / (double) CLOCKS_PER_SEC; - num_cuda = num_cuda + 1.0; - #endif current_cost = cost[1]; return cost[0]; } @@ -810,16 +786,13 @@ class gl_spider : public virtual gl_texture int getCost(GLuint tID, int n) { - #ifdef TESTING - start = std::clock(); + #ifdef TIMING + gpuStartTimer(); #endif stim::vec cost = stim::cuda::get_cost(tID, GL_TEXTURE_2D, n, 2*t_length, t_length); - cudaDeviceSynchronize(); - #ifdef TESTING - duration_cuda = duration_cuda + - (std::clock() - start) / (double) CLOCKS_PER_SEC; - num_cuda = num_cuda + 1.0; + #ifdef TIMING + cost_time += gpuStopTimer(); #endif current_cost = cost[1]; return cost[0]; @@ -839,13 +812,9 @@ class gl_spider : public virtual gl_texture //vectical rectangle forming the spider. stim::rect ver; - //Testing and Timing variables. + //Timing variables. #ifdef TESTING std::clock_t start; - double duration_sampling = 0.0; - double duration_cuda = 0.0; - double num_sampling = 0.0; - double num_cuda = 0.0; #endif //--------------------------------------------------------------------------// @@ -939,10 +908,13 @@ class gl_spider : public virtual gl_texture texID = id; //GenerateFBO(16, numSamples*8); GenerateFBO(t_length*2, numSamples*t_length, texbufferID, fboID); + std::cout << numSamples << std::endl; CHECK_OPENGL_ERROR GenerateFBO(t_length*2, numSamplesPos*t_length, ptexbufferID, pfboID); + std::cout << numSamplesPos << std::endl; CHECK_OPENGL_ERROR GenerateFBO(t_length*2, numSamplesMag*t_length, mtexbufferID, mfboID); + std::cout << numSamplesMag << std::endl; CHECK_OPENGL_ERROR GenerateFBO(16, 216, btexbufferID, bfboID); CHECK_OPENGL_ERROR @@ -1204,6 +1176,47 @@ class gl_spider : public virtual gl_texture return seeds; } + ///sets the number of direction templates. + void + setNumberDirectionTemplates(int n) + { + numSamples = n; + } + + ///sets the number of position templates. + void + setNumberPositionTemplates(int n) + { + numSamplesPos = n; + } + + ///sets the number of position templates. + void + setNumberSizeTemplates(int n) + { + numSamplesMag = n; + } + + #ifdef TIMING + ///Returns the timings at the moment the method is called. + ///In the following order: Branch, Direction, Position, Size, Cost, Network, Hit_detetion. + std::vector + getTimings() + { + std::vector ret; + ret.resize(7); + ret[0] = branch_time; + ret[1] = direction_time; + ret[2] = position_time; + ret[3] = size_time; + ret[4] = cost_time; + ret[5] = network_time; + ret[6] = hit_time; + + return ret; + } + #endif + ///returns true if all seed stacks are empty, else false. bool Empty() @@ -1302,9 +1315,6 @@ class gl_spider : public virtual gl_texture { Bind(texbufferID, fboID, numSamples, t_length); CHECK_OPENGL_ERROR - #ifdef TESTING - start = std::clock(); - #endif findOptimalDirection(); Unbind(); Bind(ptexbufferID, pfboID, numSamplesPos, t_length); @@ -1315,12 +1325,6 @@ class gl_spider : public virtual gl_texture Unbind(); CHECK_OPENGL_ERROR - #ifdef TESTING - duration_sampling = duration_sampling + - (std::clock() - start) / (double) CLOCKS_PER_SEC; - num_sampling = num_sampling + 1.0; - #endif - std::cout << current_cost << std::endl; return current_cost; } @@ -1337,6 +1341,39 @@ class gl_spider : public virtual gl_texture //--------------------------------------------------------------------------// void + MonteCarloDirectionVectors(int nSamples, float solidAngle = 2*M_PI) + { + float PHI[2], Z[2], range; + PHI[0] = asin(solidAngle/2); + PHI[1] = asin(0); + + Z[0] = cos(PHI[0]); + Z[1] = cos(PHI[1]); + + + range = Z[0] - Z[1]; + + + float z, theta, phi; + + std::vector > vecsUni; + for(int i = 0; i < numSamplesPos; i++) + { + stim::vec3 a(uniformRandom()*0.8, uniformRandom()*0.8, 0.0); + a[0] = a[0]-0.4; + a[1] = a[1]-0.4; + vecsUni.push_back(a); + } + + stringstream name; + for(int i = 0; i < numSamplesPos; i++) + name << vecsUni[i].str() << std::endl; + + std::ofstream outFile; + outFile.open("New_Pos_Vectors.txt"); + outFile << name.str().c_str(); + } + void DrawCylinder() { glNewList(dList+3, GL_COMPILE); @@ -1442,6 +1479,10 @@ class gl_spider : public virtual gl_texture selectObject(stim::vec3 loc, stim::vec3 dir, float mag) { //Define the varibles and turn on Selection Mode + + #ifdef TIMING + gpuStartTimer(); + #endif float s = 3.0; GLuint selectBuf[2048]; @@ -1501,6 +1542,10 @@ class gl_spider : public virtual gl_texture // glEnable(GL_CULL_FACE); hits = glRenderMode(GL_RENDER); int found_hits = processHits(hits, selectBuf); + #ifdef TIMING + hit_time += gpuStopTimer(); + #endif + return found_hits; } @@ -1548,6 +1593,10 @@ class gl_spider : public virtual gl_texture addToNetwork(pair, int> in, stim::vec3 spos, stim::vec smag, stim::vec3 sdir) { + #ifdef TIMING + double s = std::clock(); + #endif + std::vector > ce = in.first.centerline(); std::vector > cm = in.first.centerlinemag(); //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 } else { nt.addEdge(ce,cm, -1, -1);} } } + + #ifdef TIMING + double network_time = (std::clock() - s) / (double) CLOCKS_PER_SEC; + network_time += network_time * 1000.0; + #endif } diff --git a/stim/image/image.h b/stim/image/image.h index 5fc8120..d3f5f92 100644 --- a/stim/image/image.h +++ b/stim/image/image.h @@ -193,6 +193,7 @@ public: get_interleaved_bgr(buffer); cv::Mat cvImage((int)Y(), (int)X(), cv_type(), buffer); cv::imwrite(filename, cvImage); + free(buffer); } void set_interleaved(T* buffer, size_t width, size_t height, size_t channels){ -- libgit2 0.21.4