Commit a18fe1629f7950f34c604fa4ef6ae0c882efa3a5

Authored by David Mayerich
2 parents 1a55a328 42145f38

Merge branch 'master' of git.stim.ee.uh.edu:codebase/stimlib

Showing 2 changed files with 99 additions and 61 deletions   Show diff stats
stim/cuda/cost.h
... ... @@ -2,10 +2,11 @@
2 2 #include <cuda.h>
3 3 #include <cuda_runtime.h>
4 4 #include <cublas_v2.h>
5   -#include "cuPrintf.cu"
6   -#include "cuPrintf.cuh"
  5 +//#include "cuPrintf.cu"
  6 +//#include "cuPrintf.cuh"
7 7 #include <stdio.h>
8 8 #include "../visualization/colormap.h"
  9 +#include <sstream>
9 10  
10 11 #define DIM_Y 10890
11 12 #define DIM_X 20
... ... @@ -37,10 +38,6 @@ float get_sum(float *diff)
37 38 ret = cublasCreate(&handle);
38 39  
39 40 ret = cublasSetVector(20*10, sizeof(*diff), diff, 1, v_dif, 1);
40   - if(!testing){
41   - stim::gpu2image<float>(v_dif, "sample0.bmp", 20,10,0,1);
42   - testing = true;
43   - } //float* out = (float*) malloc(sizeof(float));
44 41 float out;
45 42 ret = cublasSasum(handle, 20*10, v_dif, 1, &out);
46 43 cublasDestroy(handle);
... ... @@ -122,11 +119,12 @@ void cleanUP(cudaGraphicsResource_t src)
122 119 }
123 120  
124 121 extern "C"
125   -int get_cost(cudaGraphicsResource_t src)
  122 +int get_cost(cudaGraphicsResource_t src, int inter)
126 123 {
127 124 float output[1089];
128 125 float mini = 10000000000000000.0;
129 126 int idx;
  127 + stringstream name;
130 128 initArray(src);
131 129 dim3 grid(20, 10890);
132 130 dim3 block(1, 1);
... ... @@ -135,11 +133,17 @@ int get_cost(cudaGraphicsResource_t src)
135 133 stim::gpu2image<float>(result, "test.bmp", 20,10890,0,1);
136 134 for (int i = 0; i < 1089; i++){
137 135 output[i] = get_sum(result+(20*10*i));
138   - if(output[i] < mini){
  136 + if(output[i] <= mini){
139 137 mini = output[i];
140 138 idx = i;
141   - }
142   - }
  139 +// if(!testing)
  140 +
  141 +// testing = true;
  142 + } //float* out = (float*) malloc(sizeof(float));
  143 + }
  144 + name << "sample_" << inter << "_" << idx << ".bmp";
  145 + output[idx] = get_sum(result+(20*10*idx));
  146 + stim::gpu2image<float>(v_dif, name.str(), 20,10,0,1);
143 147 std::cout << output[0] << std::endl;
144 148 std::cout << output[100] << std::endl;
145 149 std::cout << output[500] << std::endl;
... ...
stim/gl/gl_spider.h
... ... @@ -7,7 +7,7 @@
7 7 #include <cuda_gl_interop.h>
8 8 #include <cudaGL.h>
9 9 #include <math.h>
10   -#include "gl_texture.h"
  10 +#include "../gl/gl_texture.h"
11 11 #include "../visualization/camera.h"
12 12 #include "./error.h"
13 13 #include "../math/vector.h"
... ... @@ -15,6 +15,9 @@
15 15 #include "../cuda/cost.h"
16 16 #include "../cuda/glbind.h"
17 17  
  18 +#include <iostream>
  19 +#include <fstream>
  20 +
18 21 namespace stim
19 22 {
20 23  
... ... @@ -24,7 +27,6 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
24 27 //doen't use gl_texture really, just needs the GLuint id.
25 28 //doesn't even need the texture iD really.
26 29 private:
27   - stim::camera rotator;
28 30 stim::vec<float> position; //vector designating the position of the spider.
29 31 stim::vec<float> direction; //vector designating the orientation of the spider
30 32 //always a unit vector.
... ... @@ -32,7 +34,6 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
32 34 //mag[0] = length.
33 35 //mag[1] = width.
34 36 using gl_texture<T>::texID;
35   - //float (*S)[][];
36 37 //using image_stack<T>::S;
37 38 cudaArray* c_Array;
38 39 //void** devPtr;
... ... @@ -40,14 +41,7 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
40 41 cudaGraphicsResource_t resource;
41 42 GLuint fboID;
42 43 GLuint texbufferID;
43   -
44   - void
45   - findOptimalDirection()
46   - {
47   - /* Method for finding the best direction for the spider.
48   - Uses the camera to rotate. Then Calls Evaluate to find new cost.
49   - */
50   - }
  44 + int iter = 0;
51 45  
52 46 void
53 47 findOptimalPosition()
... ... @@ -209,12 +203,14 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
209 203 {
210 204 GenerateFBO(20, numSamples*10);
211 205  
  206 + ofstream file;
  207 + file.open("samples.txt", std::ios_base::app);
212 208 float samples[numSamples][3]; //Set up the variables
213 209 //necessary for sample generation
214 210 vec<float> d_s = in.cart2sph();
215 211 vec<float> temp;
216 212 int dim = (sqrt(numSamples)-1)/2;
217   - std::cout << dim << std::endl;
  213 + //std::cout << dim << std::endl;
218 214 float y_0 = 0.0;
219 215 float len = 10.0;
220 216 float p0 = M_PI/3;
... ... @@ -244,22 +240,26 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
244 240 glEnable(GL_TEXTURE_3D);
245 241 glBindTexture(GL_TEXTURE_3D, texID);
246 242  
  243 + //file << "iteration: " << iter << "\n";
  244 + //file << "starting pos and dir:" << "[" << position[0] << "," <<position[1] << "," << position[2] << "]" << ":" << "[" << direction[0] << "," << direction[1] << "," << direction[2] << "]\n";
247 245 //Loop over the samples
  246 + file << position[0] << "," <<position[1] << "," << position[2] << "\n";
248 247 int idx;
249 248 for(int i = -dim; i <= dim; i++){
250 249 for(int j = -dim; j <= dim; j++){
251 250 //Create linear index
252 251 idx = (i+dim)*(dim*2+1) + (j+dim);
253   - // std::cerr << i+dim << "," << j+dim << ":" << idx << std::endl;
254 252  
255   - temp[0] = 1; //rotate vector
  253 + temp[0] = d_s[0]; //rotate vector
256 254 temp[1] = d_s[1]+dt*i;
257 255 temp[2] = d_s[2]+dp*j;
258 256  
259   - temp = temp.sph2cart(); //back to cart
  257 + temp = (temp.sph2cart()).norm(); //back to cart
260 258 samples[idx][0] = temp[0]; //save sample vector
261 259 samples[idx][1] = temp[1];
262 260 samples[idx][2] = temp[2];
  261 +
  262 + //file << idx << ":" <<"[" << samples[idx][0] << "," << samples[idx][1] << "," << samples[idx][2] << "]" << "\n";
263 263  
264 264 Update(0.0, y_0+(idx)*10, temp);
265 265 }
... ... @@ -269,6 +269,58 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
269 269 glDisable(GL_TEXTURE_3D);
270 270 glBindFramebuffer(GL_FRAMEBUFFER,0);
271 271 glBindTexture(GL_TEXTURE_2D, 0);
  272 + int nxt = getCost();
  273 + stim::vec<float> next;
  274 + next[0] = samples[nxt][0];
  275 + next[1] = samples[nxt][1];
  276 + next[2] = samples[nxt][2];
  277 + next.norm();
  278 + //file << "next direction" << "[" << next[0] << "," << next[1] << "," << next[2] << "]\n\n";
  279 + setPosition(position[0] + next[0]/500,
  280 + position[1]+next[1]/500,
  281 + position[2]+next[2]/500);
  282 + setDirection(next[0], next[1], next[2]);
  283 + //file.close();
  284 + }
  285 +
  286 +//--------------------------------------------------------------------------//
  287 +//--------------------------------CUDA METHODS------------------------------//
  288 +//--------------------------------------------------------------------------//
  289 +
  290 + /* Method for registering the texture with Cuda for shared
  291 + access */
  292 + void
  293 + createResource()
  294 + {
  295 + HANDLE_ERROR(
  296 + cudaGraphicsGLRegisterImage(
  297 + &resource,
  298 + texbufferID,
  299 + GL_TEXTURE_2D,
  300 + //CU_GRAPHICS_REGISTER_FLAGS_NONE)
  301 + cudaGraphicsMapFlagsReadOnly)
  302 + );
  303 + }
  304 +
  305 + /* Method for freeing the texture from Cuda for gl access */
  306 + void
  307 + destroyResource()
  308 + {
  309 + HANDLE_ERROR(
  310 + cudaGraphicsUnregisterResource(resource)
  311 + );
  312 + }
  313 +
  314 + /* Entry-point into the cuda code for calculating the cost
  315 + of a given samples array (in texture form) */
  316 + int
  317 + getCost()
  318 + {
  319 + createResource();
  320 + int cost = get_cost(resource, iter);
  321 + destroyResource();
  322 + iter++;
  323 + return cost;
272 324 }
273 325  
274 326 public:
... ... @@ -403,8 +455,9 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
403 455 {
404 456 std::cout << position[0] << "," << position[1] << "," << position[1]
405 457 << std::endl;
406   - setPosition(direction*magnitude[1]/2+position);
407   - Update();
  458 + //setPosition(direction*magnitude[1]/2+position);
  459 + findOptimalDirection();
  460 + //Update();
408 461 std::cout << position[0] << "," << position[1] << "," << position[1]
409 462 << std::endl;
410 463  
... ... @@ -536,44 +589,25 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
536 589  
537 590  
538 591  
  592 +
539 593 void
540   - initCuda()
541   - {
542   - stim::cudaSetDevice();
543   - GLint max;
544   - glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
545   - std::cout << max << std::endl;
546   - }
547   -
548   - void
549   - createResource()
550   - {
551   - HANDLE_ERROR(
552   - cudaGraphicsGLRegisterImage(
553   - &resource,
554   - texbufferID,
555   - GL_TEXTURE_2D,
556   - //CU_GRAPHICS_REGISTER_FLAGS_NONE)
557   - cudaGraphicsMapFlagsReadOnly)
558   - );
559   - }
560   -
561   - void
562   - destroyResource()
  594 + findOptimalDirection()
563 595 {
564   - HANDLE_ERROR(
565   - cudaGraphicsUnregisterResource(resource)
566   - );
  596 + /* Method for finding the best direction for the spider.
  597 + Uses the camera to rotate. Then Calls Evaluate to find new cost.
  598 + */
  599 + Sample(direction);
567 600 }
568 601  
569   -
570   - int
571   - getCost()
572   - {
573   - createResource();
574   - int cost = get_cost(resource);
575   - destroyResource();
576   - return cost;
  602 + /* Method for initializing the cuda devices, necessary only
  603 + there are multiple cuda devices */
  604 + void
  605 + initCuda()
  606 + {
  607 + //stim::cudaSetDevice();
  608 + //GLint max;
  609 + //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
  610 + //std::cout << max << std::endl;
577 611 }
578 612 };
579 613 }
... ...