From bb457896ff0324f28e9459f04fc331796dcc208a Mon Sep 17 00:00:00 2001 From: David Mayerich Date: Fri, 3 Mar 2017 15:36:42 -0600 Subject: [PATCH] improved error messages, defaulted to read-only ENVI files --- stim/envi/agilent_binary.h | 25 ++++++++++++++++++++++++- stim/envi/bil.h | 5 +++-- stim/envi/binary.h | 24 ++++++++++++++---------- stim/envi/bip.h | 5 +++-- stim/envi/bsq.h | 5 +++-- stim/envi/envi.h | 15 +++++++++------ 6 files changed, 56 insertions(+), 23 deletions(-) diff --git a/stim/envi/agilent_binary.h b/stim/envi/agilent_binary.h index 042db2b..377b4c7 100644 --- a/stim/envi/agilent_binary.h +++ b/stim/envi/agilent_binary.h @@ -225,7 +225,7 @@ public: //#ifdef CUDA_FOUND /// Perform an FFT and return a binary file with bands in the specified range - agilent_binary fft(double band_min, double band_max, double ELWN = 15798, int UDR = 2){ + agilent_binary fft(double band_min, double band_max, double ELWN = 15798, int UDR = 2, int device = 0){ auto total_start = std::chrono::high_resolution_clock::now(); auto start = std::chrono::high_resolution_clock::now(); @@ -240,6 +240,29 @@ public: // std::cout << "Transpose data: " << diff.count() << " s\n"; start = std::chrono::high_resolution_clock::now(); + if (device >= 0) { //if a CUDA device is specified + int dev_count; + HANDLE_ERROR(cudaGetDeviceCount(&dev_count)); //get the number of CUDA devices + std::cout << "Number of CUDA devices: " << dev_count << std::endl; //output the number of CUDA devices + cudaDeviceProp prop; + std::cout << "CUDA devices----" << std::endl; + for (int d = 0; d < dev_count; d++) { //for each CUDA device + cudaGetDeviceProperties(&prop, d); //get the property of the first device + //float cc = prop.major + prop.minor / 10.0f; //calculate the compute capability + std::cout << d << ": [" << prop.major << "." << prop.minor << "] " << prop.name << std::endl; //display the device information + //if(cc > best_device_cc){ + // best_device_cc = cc; //if this is better than the previous device, use it + // best_device_id = d; + //} + } + if (dev_count > 0 && dev_count > device) { //if the first device is not an emulator + cudaGetDeviceProperties(&prop, device); //get the property of the requested CUDA device + if (prop.major != 9999) { + std::cout << "Using device " << device << std::endl; + HANDLE_ERROR(cudaSetDevice(device)); + } + } + } cufftHandle plan; //allocate space for a cufft plan cufftReal* gpu_data; //create a pointer to the data size_t batch = R[0] * R[1]; //calculate the batch size (X * Y) diff --git a/stim/envi/bil.h b/stim/envi/bil.h index e4d3efe..6e03dc9 100644 --- a/stim/envi/bil.h +++ b/stim/envi/bil.h @@ -55,11 +55,12 @@ public: unsigned long long Y, unsigned long long B, unsigned long long header_offset, - std::vector wavelengths){ + std::vector wavelengths, + stim::iotype io = stim::io_in){ w = wavelengths; - return open(filename, vec(X, B, Y), header_offset); + return open(filename, vec(X, B, Y), header_offset, io); } diff --git a/stim/envi/binary.h b/stim/envi/binary.h index 1c0dc6c..3513e0b 100644 --- a/stim/envi/binary.h +++ b/stim/envi/binary.h @@ -143,6 +143,8 @@ public: * @param T is the data type used to store data to disk (generally float or double) * @param D is the dimension of the data (default 3) */ + +enum iotype {io_in, io_out}; template< typename T, unsigned int D = 3 > class binary{ @@ -205,19 +207,21 @@ protected: /// Private helper file that opens a specified binary file. /// @param filename is the name of the binary file to stream - bool open_file(std::string filename){ + bool open_file(std::string filename, stim::iotype io = io_in){ //open the file as binary for reading and writing - file.open(filename.c_str(), std::ios::in | std::ios::out | std::ios::binary); - - //if the file isn't open, the user may only have read access - if(!file.is_open()){ - std::cout<<"class STIM::BINARY - failed to open file, trying for read only"< r, unsigned long long h = 0){ + bool open(std::string filename, vec r, unsigned long long h = 0, stim::iotype io = stim::io_in){ for(unsigned long long i = 0; i < D; i++) //set the dimensions of the binary file object R[i] = r[i]; header = h; //save the header size - if(!open_file(filename)) return false; //open the binary file + if(!open_file(filename), io) return false; //open the binary file //reset(); diff --git a/stim/envi/bip.h b/stim/envi/bip.h index 215ff38..299ad99 100644 --- a/stim/envi/bip.h +++ b/stim/envi/bip.h @@ -65,14 +65,15 @@ public: unsigned long long Y, unsigned long long B, unsigned long long header_offset, - std::vector wavelengths){ + std::vector wavelengths, + stim::iotype io = stim::io_in){ //copy the wavelengths to the BSQ file structure w = wavelengths; //copy the offset to the structure offset = header_offset; - return open(filename, vec(B, X, Y), header_offset); + return open(filename, vec(B, X, Y), header_offset, io); } diff --git a/stim/envi/bsq.h b/stim/envi/bsq.h index 63f5b39..08600a9 100644 --- a/stim/envi/bsq.h +++ b/stim/envi/bsq.h @@ -64,14 +64,15 @@ public: unsigned long long Y, unsigned long long B, unsigned long long header_offset, - std::vector wavelengths){ + std::vector wavelengths, + stim::iotype io = stim::io_in){ //copy the wavelengths to the BSQ file structure w = wavelengths; //copy the wavelengths to the structure offset = header_offset; - return open(filename, vec(X, Y, B), header_offset); + return open(filename, vec(X, Y, B), header_offset, io); } /// Retrieve a single band (based on index) and stores it in pre-allocated memory. diff --git a/stim/envi/envi.h b/stim/envi/envi.h index 1278ec7..4de82d3 100644 --- a/stim/envi/envi.h +++ b/stim/envi/envi.h @@ -363,7 +363,7 @@ public: } /// Open a previously opened ENVI file - bool open(){ + bool open(stim::iotype io = stim::io_in){ //load the file if(header.interleave == envi_header::BSQ) { //if the infile is bsq file @@ -451,7 +451,7 @@ public: /// @param filename is the name of the ENVI binary file /// @param header is an ENVI header structure - bool open(std::string filename, stim::envi_header h){ + bool open(std::string filename, stim::envi_header h, stim::iotype io = stim::io_in){ header = h; //store the header @@ -459,7 +459,7 @@ public: allocate(); - return open(); //open the ENVI file; + return open(io); //open the ENVI file; } @@ -468,18 +468,21 @@ public: /// @param filename is the name of the ENVI binary file /// @param headername is the name of the ENVI header file - bool open(std::string filename, std::string headername){ + bool open(std::string filename, std::string headername, stim::iotype io = stim::io_in){ //allocate memory //allocate(); stim::envi_header h; - h.load(headername); + if (!h.load(headername)) { + std::cout << "Error loading header file: " << headername << std::endl; + return false; + } //load the header //header.load(headername); - return open(filename, h); + return open(filename, h, io); } /// Normalize a hyperspectral ENVI file given a band number and threshold. -- libgit2 0.21.4