1 #include <iostream>
2 #include <vcl_where_root_dir.h>
3 #include "testlib/testlib_test.h"
4 #ifdef _MSC_VER
5 #  include "vcl_msvc_warnings.h"
6 #endif
7 #include "vnl/vnl_vector.h"
8 #include <vnl/algo/vnl_cholesky.h>
9 #include <bocl/bocl_manager.h>
10 #include <bocl/bocl_kernel.h>
11 #include <bocl/bocl_mem.h>
12 #include <bocl/bocl_device.h>
13 #include <boxm2/ocl/tests/boxm2_ocl_test_utils.h>
14 
ocl_levenberg_marquardt(vnl_vector<float> x,vnl_vector<float> y,int m,int n,int)15 void ocl_levenberg_marquardt(vnl_vector<float> x,
16                              vnl_vector<float> y,
17                              int m, int n, int  /*max_iter*/)
18 {
19   //load BOCL stuff
20   bocl_manager_child &mgr = bocl_manager_child::instance();
21   if (mgr.gpus_.size()==0)   return;
22   bocl_device_sptr device = mgr.gpus_[0];
23 
24   //compile pyramid test
25   std::vector<std::string> src_paths;
26   std::string source_dir = std::string(VCL_SOURCE_ROOT_DIR) + "/contrib/brl/bseg/boxm2/ocl/cl/";
27   src_paths.push_back(source_dir + "onl/test_levenberg_marquardt.cl");
28   src_paths.push_back(source_dir + "onl/cholesky_decomposition.cl");
29   src_paths.push_back(source_dir + "onl/quadratic_example.cl");
30   src_paths.push_back(source_dir + "onl/levenberg_marquardt.cl");
31 
32 
33   bocl_kernel lm_test;
34   lm_test.create_kernel(&device->context(),device->device_id(), src_paths, "test_levenberg_marquardt", "-D QUADRATIC", "test levenberg marquardt");
35 
36   // create a command queue.
37   int status=0;
38   cl_command_queue queue = clCreateCommandQueue(device->context(),
39                                                 *(device->device_id()),
40                                                 CL_QUEUE_PROFILING_ENABLE,&status);
41 
42   //create all the arguments
43   bocl_mem_sptr xbuff = new bocl_mem( device->context(), x.data_block(), x.size()*sizeof(float), "x vector");
44   xbuff->create_buffer(CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR);
45 
46   bocl_mem_sptr ybuff = new bocl_mem( device->context(), y.data_block(), y.size()*sizeof(float), "y vector");
47   ybuff->create_buffer(CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR);
48 
49   bocl_mem_sptr mbuff = new bocl_mem( device->context(), &m, sizeof(int), "dimension of x");
50   mbuff->create_buffer(CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR);
51 
52   bocl_mem_sptr nbuff = new bocl_mem( device->context(), &n, sizeof(int), "dimension of y");
53   nbuff->create_buffer(CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR);
54 
55   int maxiter = 100;
56   bocl_mem_sptr maxiterbuff = new bocl_mem( device->context(), &maxiter, sizeof(int), "Maximum # of iterations");
57   maxiterbuff->create_buffer(CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR);
58 
59   float output[1000];
60   bocl_mem_sptr outputbuff = new bocl_mem( device->context(), output, 1000* sizeof(float), "Vector for Output");
61   outputbuff->create_buffer(CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR);
62 
63   //set workspace
64   std::size_t lThreads[] = {16};
65   std::size_t gThreads[] = {16};
66   //set first kernel args
67   lm_test.set_arg( maxiterbuff.ptr() );
68   lm_test.set_arg( nbuff.ptr() );
69   lm_test.set_arg( mbuff.ptr() );
70   lm_test.set_arg( xbuff.ptr() );
71   lm_test.set_arg( ybuff.ptr() );
72   lm_test.set_arg( outputbuff.ptr() );
73   lm_test.set_local_arg( x.size()*sizeof(float) );          //local tree,
74   lm_test.set_local_arg( x.size()*sizeof(float) );          //local tree,
75   lm_test.set_local_arg( y.size()*sizeof(float) );          //local tree,
76   lm_test.set_local_arg( x.size()*sizeof(float) );          //local tree,
77   lm_test.set_local_arg( x.size()*y.size()*sizeof(float) ); //local tree,
78   lm_test.set_local_arg( x.size()*x.size()*sizeof(float) ); //local tree,
79   lm_test.set_local_arg( y.size()*sizeof(float) );          //local tree,
80   lm_test.set_local_arg( x.size()*sizeof(float) );          //local tree,
81   //execute kernel
82   lm_test.execute( queue, 1, lThreads, gThreads);
83   clFinish( queue );
84   outputbuff->read_to_buffer(queue);
85 
86   for ( unsigned i = 0 ; i < 10; i++)
87     std::cout<<output[i]<<' ';
88 }
89 
test_ocl_levenberg_marquardt()90 void test_ocl_levenberg_marquardt()
91 {
92   vnl_vector<float>  x(3) ;
93   x[0] = 1.0;
94   x[1] = 2.0;
95   x[2] = 7.0;
96   int n = x.size();
97 
98   vnl_vector<float>  y(2) ;
99 
100   y[0] = 2.0;
101   y[1] = 2.0;
102   int m = y.size();
103 
104   int max_iter = 100;
105   ocl_levenberg_marquardt(x,y,m,n,max_iter);
106 }
107 
108 
109 TESTMAIN(test_ocl_levenberg_marquardt);
110