diff --git a/stim/cuda/cost.h b/stim/cuda/cost.h index db5b7c2..6e24333 100644 --- a/stim/cuda/cost.h +++ b/stim/cuda/cost.h @@ -2,10 +2,11 @@ #include #include #include -#include "cuPrintf.cu" -#include "cuPrintf.cuh" +//#include "cuPrintf.cu" +//#include "cuPrintf.cuh" #include #include "../visualization/colormap.h" +#include #define DIM_Y 10890 #define DIM_X 20 @@ -37,10 +38,6 @@ float get_sum(float *diff) ret = cublasCreate(&handle); ret = cublasSetVector(20*10, sizeof(*diff), diff, 1, v_dif, 1); - if(!testing){ - stim::gpu2image(v_dif, "sample0.bmp", 20,10,0,1); - testing = true; - } //float* out = (float*) malloc(sizeof(float)); float out; ret = cublasSasum(handle, 20*10, v_dif, 1, &out); cublasDestroy(handle); @@ -122,11 +119,12 @@ void cleanUP(cudaGraphicsResource_t src) } extern "C" -int get_cost(cudaGraphicsResource_t src) +int get_cost(cudaGraphicsResource_t src, int inter) { float output[1089]; float mini = 10000000000000000.0; int idx; + stringstream name; initArray(src); dim3 grid(20, 10890); dim3 block(1, 1); @@ -135,11 +133,17 @@ int get_cost(cudaGraphicsResource_t src) stim::gpu2image(result, "test.bmp", 20,10890,0,1); for (int i = 0; i < 1089; i++){ output[i] = get_sum(result+(20*10*i)); - if(output[i] < mini){ + if(output[i] <= mini){ mini = output[i]; idx = i; - } - } +// if(!testing) + +// testing = true; + } //float* out = (float*) malloc(sizeof(float)); + } + name << "sample_" << inter << "_" << idx << ".bmp"; + output[idx] = get_sum(result+(20*10*idx)); + stim::gpu2image(v_dif, name.str(), 20,10,0,1); std::cout << output[0] << std::endl; std::cout << output[100] << std::endl; std::cout << output[500] << std::endl; diff --git a/stim/gl/gl_spider.h b/stim/gl/gl_spider.h index c28c257..0266563 100644 --- a/stim/gl/gl_spider.h +++ b/stim/gl/gl_spider.h @@ -7,7 +7,7 @@ #include #include #include -#include "gl_texture.h" +#include "../gl/gl_texture.h" #include "../visualization/camera.h" #include "./error.h" #include "../math/vector.h" @@ -15,6 +15,9 @@ #include "../cuda/cost.h" #include "../cuda/glbind.h" +#include +#include + namespace stim { @@ -24,7 +27,6 @@ class gl_spider : public virtual gl_texture //doen't use gl_texture really, just needs the GLuint id. //doesn't even need the texture iD really. private: - stim::camera rotator; stim::vec position; //vector designating the position of the spider. stim::vec direction; //vector designating the orientation of the spider //always a unit vector. @@ -32,7 +34,6 @@ class gl_spider : public virtual gl_texture //mag[0] = length. //mag[1] = width. using gl_texture::texID; - //float (*S)[][]; //using image_stack::S; cudaArray* c_Array; //void** devPtr; @@ -40,14 +41,7 @@ class gl_spider : public virtual gl_texture cudaGraphicsResource_t resource; GLuint fboID; GLuint texbufferID; - - void - findOptimalDirection() - { - /* Method for finding the best direction for the spider. - Uses the camera to rotate. Then Calls Evaluate to find new cost. - */ - } + int iter = 0; void findOptimalPosition() @@ -209,12 +203,14 @@ class gl_spider : public virtual gl_texture { GenerateFBO(20, numSamples*10); + ofstream file; + file.open("samples.txt", std::ios_base::app); float samples[numSamples][3]; //Set up the variables //necessary for sample generation vec d_s = in.cart2sph(); vec temp; int dim = (sqrt(numSamples)-1)/2; - std::cout << dim << std::endl; + //std::cout << dim << std::endl; float y_0 = 0.0; float len = 10.0; float p0 = M_PI/3; @@ -244,22 +240,26 @@ class gl_spider : public virtual gl_texture glEnable(GL_TEXTURE_3D); glBindTexture(GL_TEXTURE_3D, texID); + //file << "iteration: " << iter << "\n"; + //file << "starting pos and dir:" << "[" << position[0] << "," < glDisable(GL_TEXTURE_3D); glBindFramebuffer(GL_FRAMEBUFFER,0); glBindTexture(GL_TEXTURE_2D, 0); + int nxt = getCost(); + stim::vec next; + next[0] = samples[nxt][0]; + next[1] = samples[nxt][1]; + next[2] = samples[nxt][2]; + next.norm(); + //file << "next direction" << "[" << next[0] << "," << next[1] << "," << next[2] << "]\n\n"; + setPosition(position[0] + next[0]/500, + position[1]+next[1]/500, + position[2]+next[2]/500); + setDirection(next[0], next[1], next[2]); + //file.close(); + } + +//--------------------------------------------------------------------------// +//--------------------------------CUDA METHODS------------------------------// +//--------------------------------------------------------------------------// + + /* Method for registering the texture with Cuda for shared + access */ + void + createResource() + { + HANDLE_ERROR( + cudaGraphicsGLRegisterImage( + &resource, + texbufferID, + GL_TEXTURE_2D, + //CU_GRAPHICS_REGISTER_FLAGS_NONE) + cudaGraphicsMapFlagsReadOnly) + ); + } + + /* Method for freeing the texture from Cuda for gl access */ + void + destroyResource() + { + HANDLE_ERROR( + cudaGraphicsUnregisterResource(resource) + ); + } + + /* Entry-point into the cuda code for calculating the cost + of a given samples array (in texture form) */ + int + getCost() + { + createResource(); + int cost = get_cost(resource, iter); + destroyResource(); + iter++; + return cost; } public: @@ -403,8 +455,9 @@ class gl_spider : public virtual gl_texture { std::cout << position[0] << "," << position[1] << "," << position[1] << std::endl; - setPosition(direction*magnitude[1]/2+position); - Update(); + //setPosition(direction*magnitude[1]/2+position); + findOptimalDirection(); + //Update(); std::cout << position[0] << "," << position[1] << "," << position[1] << std::endl; @@ -536,44 +589,25 @@ class gl_spider : public virtual gl_texture + void - initCuda() - { - stim::cudaSetDevice(); - GLint max; - glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max); - std::cout << max << std::endl; - } - - void - createResource() - { - HANDLE_ERROR( - cudaGraphicsGLRegisterImage( - &resource, - texbufferID, - GL_TEXTURE_2D, - //CU_GRAPHICS_REGISTER_FLAGS_NONE) - cudaGraphicsMapFlagsReadOnly) - ); - } - - void - destroyResource() + findOptimalDirection() { - HANDLE_ERROR( - cudaGraphicsUnregisterResource(resource) - ); + /* Method for finding the best direction for the spider. + Uses the camera to rotate. Then Calls Evaluate to find new cost. + */ + Sample(direction); } - - int - getCost() - { - createResource(); - int cost = get_cost(resource); - destroyResource(); - return cost; + /* Method for initializing the cuda devices, necessary only + there are multiple cuda devices */ + void + initCuda() + { + //stim::cudaSetDevice(); + //GLint max; + //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max); + //std::cout << max << std::endl; } }; } -- libgit2 0.21.4