Skip to content

Commit

Permalink
Merge pull request #405 from dirac-institute/cleanup
Browse files Browse the repository at this point in the history
Clean Up Configuration
  • Loading branch information
jeremykubica authored Dec 15, 2023
2 parents 9d54136 + 429c01c commit 66165d5
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 150 deletions.
3 changes: 1 addition & 2 deletions data/demo_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ debug: true
do_clustering: true
do_mask: true
do_stamp_filter: true
encode_phi_bytes: -1
encode_psi_bytes: -1
encode_num_bytes: -1
eps: 0.03
flag_keys:
- BAD
Expand Down
3 changes: 1 addition & 2 deletions src/kbmod/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ def __init__(self):
"do_mask": True,
"do_stamp_filter": True,
"eps": 0.03,
"encode_psi_bytes": -1,
"encode_phi_bytes": -1,
"encode_num_bytes": -1,
"flag_keys": default_flag_keys,
"gpu_filter": False,
"im_filepath": None,
Expand Down
4 changes: 2 additions & 2 deletions src/kbmod/run_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ def do_gpu_search(self, config, search):

# If we are using an encoded image representation on GPU, enable it and
# set the parameters.
if config["encode_psi_bytes"] > 0 or config["encode_phi_bytes"] > 0:
search.enable_gpu_encoding(config["encode_psi_bytes"], config["encode_phi_bytes"])
if config["encode_num_bytes"] > 0:
search.enable_gpu_encoding(config["encode_num_bytes"])

# Enable debugging.
if config["debug"]:
Expand Down
18 changes: 1 addition & 17 deletions src/kbmod/search/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ struct SearchParameters {
float sigmag_coeff;

// Use a compressed image representation.
int psi_num_bytes; // -1 (No encoding), 1 or 2
int phi_num_bytes; // -1 (No encoding), 1 or 2
int encode_num_bytes; // -1 (No encoding), 1 or 2

// The bounds on which x and y pixels can be used
// to start a search.
Expand All @@ -104,21 +103,6 @@ struct SearchParameters {
bool debug;
};

struct scaleParameters {
float min_val;
float max_val;
float scale;
};

// Search data on a per-image basis.
struct PerImageData {
int num_images = 0;

float *image_times = nullptr;
scaleParameters *psi_params = nullptr;
scaleParameters *phi_params = nullptr;
};

struct StampParameters {
int radius = 10;
StampType stamp_type = STAMP_SUM;
Expand Down
42 changes: 15 additions & 27 deletions src/kbmod/search/kernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,18 @@

namespace search {

extern "C" void device_allocate_psi_phi_array(PsiPhiArray* data) {
if (!data->cpu_array_allocated())
throw std::runtime_error("CPU data is not allocated.");
if (data->gpu_array_allocated())
throw std::runtime_error("GPU data is already allocated.");
extern "C" void device_allocate_psi_phi_array(PsiPhiArray *data) {
if (!data->cpu_array_allocated()) throw std::runtime_error("CPU data is not allocated.");
if (data->gpu_array_allocated()) throw std::runtime_error("GPU data is already allocated.");

void* device_array_ptr;
void *device_array_ptr;
checkCudaErrors(cudaMalloc((void **)&device_array_ptr, data->get_total_array_size()));
checkCudaErrors(cudaMemcpy(device_array_ptr,
data->get_cpu_array_ptr(),
data->get_total_array_size(),
checkCudaErrors(cudaMemcpy(device_array_ptr, data->get_cpu_array_ptr(), data->get_total_array_size(),
cudaMemcpyHostToDevice));
data->set_gpu_array_ptr(device_array_ptr);
}

extern "C" void device_free_psi_phi_array(PsiPhiArray* data) {
extern "C" void device_free_psi_phi_array(PsiPhiArray *data) {
if (data->gpu_array_allocated()) {
checkCudaErrors(cudaFree(data->get_gpu_array_ptr()));
data->set_gpu_array_ptr(nullptr);
Expand Down Expand Up @@ -312,8 +308,9 @@ extern "C" void deviceSearchFilter(PsiPhiArray &psi_phi_array, float *image_time
dim3 threads(THREAD_DIM_X, THREAD_DIM_Y);

// Launch Search
searchFilterImages<<<blocks, threads>>>(psi_phi_array.get_meta_data(), psi_phi_array.get_gpu_array_ptr(), device_img_times,
params, num_trajectories, device_tests, device_search_results);
searchFilterImages<<<blocks, threads>>>(psi_phi_array.get_meta_data(), psi_phi_array.get_gpu_array_ptr(),
device_img_times, params, num_trajectories, device_tests,
device_search_results);

// Read back results
checkCudaErrors(cudaMemcpy(best_results, device_search_results, sizeof(Trajectory) * num_results,
Expand All @@ -326,7 +323,7 @@ extern "C" void deviceSearchFilter(PsiPhiArray &psi_phi_array, float *image_time
}

__global__ void deviceGetCoaddStamp(int num_images, int width, int height, float *image_vect,
PerImageData image_data, int num_trajectories, Trajectory *trajectories,
float *image_times, int num_trajectories, Trajectory *trajectories,
StampParameters params, int *use_index_vect, float *results) {
// Get the trajectory that we are going to be using.
const int trj_index = blockIdx.x * blockDim.x + threadIdx.x;
Expand Down Expand Up @@ -359,7 +356,7 @@ __global__ void deviceGetCoaddStamp(int num_images, int width, int height, float
}

// Predict the trajectory's position.
float curr_time = image_data.image_times[t];
float curr_time = image_times[t];
int current_x = int(trj.x + trj.vx * curr_time);
int current_y = int(trj.y + trj.vy * curr_time);

Expand Down Expand Up @@ -422,7 +419,7 @@ __global__ void deviceGetCoaddStamp(int num_images, int width, int height, float
}

void deviceGetCoadds(const unsigned int num_images, const unsigned int width, const unsigned int height,
std::vector<float *> data_refs, PerImageData image_data, int num_trajectories,
std::vector<float *> data_refs, std::vector<float> &image_times, int num_trajectories,
Trajectory *trajectories, StampParameters params,
std::vector<std::vector<bool>> &use_index_vect, float *results) {
// Allocate Device memory
Expand Down Expand Up @@ -465,8 +462,8 @@ void deviceGetCoadds(const unsigned int num_images, const unsigned int width, co

// Allocate and copy the times.
checkCudaErrors(cudaMalloc((void **)&device_times, sizeof(float) * num_images));
checkCudaErrors(cudaMemcpy(device_times, image_data.image_times, sizeof(float) * num_images,
cudaMemcpyHostToDevice));
checkCudaErrors(
cudaMemcpy(device_times, image_times.data(), sizeof(float) * num_images, cudaMemcpyHostToDevice));

// Allocate and copy the images.
checkCudaErrors(cudaMalloc((void **)&device_img, sizeof(float) * num_image_pixels));
Expand All @@ -480,20 +477,11 @@ void deviceGetCoadds(const unsigned int num_images, const unsigned int width, co
// Allocate space for the results.
checkCudaErrors(cudaMalloc((void **)&device_res, sizeof(float) * num_stamp_pixels));

// Wrap the per-image data into a struct. This struct will be copied by value
// during the function call, so we don't need to allocate memory for the
// struct itself. We just set the pointers to the on device vectors.
PerImageData device_image_data;
device_image_data.num_images = num_images;
device_image_data.image_times = device_times;
device_image_data.psi_params = nullptr;
device_image_data.phi_params = nullptr;

dim3 blocks(num_trajectories, 1, 1);
dim3 threads(1, stamp_width, stamp_width);

// Create the stamps.
deviceGetCoaddStamp<<<blocks, threads>>>(num_images, width, height, device_img, device_image_data,
deviceGetCoaddStamp<<<blocks, threads>>>(num_images, width, height, device_img, device_times,
num_trajectories, device_trjs, params, device_use_index,
device_res);
cudaDeviceSynchronize();
Expand Down
18 changes: 6 additions & 12 deletions src/kbmod/search/stack_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ StackSearch::StackSearch(ImageStack& imstack) : stack(imstack) {
params.sigmag_coeff = -1.0;

// Default the encoding parameters.
params.psi_num_bytes = -1;
params.phi_num_bytes = -1;
params.encode_num_bytes = -1;

// Default pixel starting bounds.
params.x_start_min = 0;
Expand All @@ -47,18 +46,13 @@ void StackSearch::enable_gpu_sigmag_filter(std::vector<float> percentiles, float
params.min_lh = min_lh;
}

void StackSearch::enable_gpu_encoding(int psi_num_bytes, int phi_num_bytes) {
void StackSearch::enable_gpu_encoding(int encode_num_bytes) {
// Make sure the encoding is one of the supported options.
// Otherwise use default float (aka no encoding).
if (psi_num_bytes == 1 || psi_num_bytes == 2) {
params.psi_num_bytes = psi_num_bytes;
if (encode_num_bytes == 1 || encode_num_bytes == 2) {
params.encode_num_bytes = encode_num_bytes;
} else {
params.psi_num_bytes = -1;
}
if (phi_num_bytes == 1 || phi_num_bytes == 2) {
params.phi_num_bytes = phi_num_bytes;
} else {
params.phi_num_bytes = -1;
params.encode_num_bytes = -1;
}
}

Expand All @@ -84,7 +78,7 @@ void StackSearch::search(int ang_steps, int vel_steps, float min_ang, float max_
DebugTimer psi_phi_timer = DebugTimer("Creating psi/phi buffers", debug_info);
prepare_psi_phi();
PsiPhiArray psi_phi_data;
fill_psi_phi_array(psi_phi_data, params.psi_num_bytes, psi_images, phi_images);
fill_psi_phi_array(psi_phi_data, params.encode_num_bytes, psi_images, phi_images);
psi_phi_timer.stop();

// Allocate a vector for the results.
Expand Down
2 changes: 1 addition & 1 deletion src/kbmod/search/stack_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class StackSearch {

// The primary search functions.
void enable_gpu_sigmag_filter(std::vector<float> percentiles, float sigmag_coeff, float min_lh);
void enable_gpu_encoding(int psi_num_bytes, int phi_num_bytes);
void enable_gpu_encoding(int num_bytes);

void set_start_bounds_x(int x_min, int x_max);
void set_start_bounds_y(int y_min, int y_max);
Expand Down
18 changes: 6 additions & 12 deletions src/kbmod/search/stamp_creator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace search {
#ifdef HAVE_CUDA
void deviceGetCoadds(const unsigned int num_images, const unsigned int width, const unsigned int height,
const std::vector<float*> data_refs, PerImageData image_data, int num_trajectories,
std::vector<float*> data_refs, std::vector<float>& image_times, int num_trajectories,
Trajectory* trajectories, StampParameters params,
std::vector<std::vector<bool>>& use_index_vect, float* results);
#endif
Expand Down Expand Up @@ -43,24 +43,21 @@ std::vector<RawImage> StampCreator::get_stamps(ImageStack& stack, const Trajecto
// NO_DATA tagged (so we can filter it out of mean/median).
RawImage StampCreator::get_median_stamp(ImageStack& stack, const Trajectory& trj, int radius,
const std::vector<bool>& use_index) {
return create_median_image(
create_stamps(stack, trj, radius, true /*=keep_no_data*/, use_index));
return create_median_image(create_stamps(stack, trj, radius, true /*=keep_no_data*/, use_index));
}

// For creating coadded stamps, we do not interpolate the pixel values and keep
// NO_DATA tagged (so we can filter it out of mean/median).
RawImage StampCreator::get_mean_stamp(ImageStack& stack, const Trajectory& trj, int radius,
const std::vector<bool>& use_index) {
return create_mean_image(
create_stamps(stack, trj, radius, true /*=keep_no_data*/, use_index));
return create_mean_image(create_stamps(stack, trj, radius, true /*=keep_no_data*/, use_index));
}

// For creating summed stamps, we do not interpolate the pixel values and replace NO_DATA
// with zero (which is the same as filtering it out for the sum).
RawImage StampCreator::get_summed_stamp(ImageStack& stack, const Trajectory& trj, int radius,
const std::vector<bool>& use_index) {
return create_summed_image(
create_stamps(stack, trj, radius, false /*=keep_no_data*/, use_index));
return create_summed_image(create_stamps(stack, trj, radius, false /*=keep_no_data*/, use_index));
}

std::vector<RawImage> StampCreator::get_coadded_stamps(ImageStack& stack, std::vector<Trajectory>& t_array,
Expand Down Expand Up @@ -168,9 +165,6 @@ std::vector<RawImage> StampCreator::get_coadded_stamps_gpu(ImageStack& stack,

// Create a data stucture for the per-image data.
std::vector<float> image_times = stack.build_zeroed_times();
PerImageData img_data;
img_data.num_images = num_images;
img_data.image_times = image_times.data();

// Allocate space for the results.
const int num_trajectories = t_array.size();
Expand All @@ -194,8 +188,8 @@ std::vector<RawImage> StampCreator::get_coadded_stamps_gpu(ImageStack& stack,
data_refs[t] = sci.data();
}

deviceGetCoadds(num_images, width, height, data_refs, img_data, num_trajectories, t_array.data(), params,
use_index_vect, stamp_data.data());
deviceGetCoadds(num_images, width, height, data_refs, image_times, num_trajectories, t_array.data(),
params, use_index_vect, stamp_data.data());
#else
throw std::runtime_error("Non-GPU co-adds is not implemented.");
#endif
Expand Down
3 changes: 1 addition & 2 deletions tests/diff_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ def perform_search(im_filepath, time_file, psf_file, res_filepath, res_suffix, s
"repeated_flag_keys": repeated_flag_keys,
"known_obj_thresh": None,
"known_obj_jpl": False,
"encode_psi_bytes": -1,
"encode_phi_bytes": -1,
"encode_num_bytes": -1,
}

rs = SearchRunner()
Expand Down
3 changes: 1 addition & 2 deletions tests/test_analysis_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ def setUp(self):
"mask_bits_dict": self.default_mask_bits_dict,
"flag_keys": self.default_flag_keys,
"repeated_flag_keys": self.default_repeated_flag_keys,
"encode_psi_bytes": -1,
"encode_phi_bytes": -1,
"encode_num_bytes": -1,
"known_obj_thresh": None,
"known_obj_jpl": False,
}
Expand Down
12 changes: 6 additions & 6 deletions tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ def test_validate(self):
def test_set(self):
config = SearchConfiguration()
self.assertIsNone(config["im_filepath"])
self.assertEqual(config["encode_psi_bytes"], -1)
self.assertEqual(config["encode_num_bytes"], -1)

config.set("im_filepath", "Here")
config.set("encode_psi_bytes", 2)
config.set("encode_num_bytes", 2)
self.assertEqual(config["im_filepath"], "Here")
self.assertEqual(config["encode_psi_bytes"], 2)
self.assertEqual(config["encode_num_bytes"], 2)

# The set should fail when using unknown parameters and strict checking.
self.assertRaises(KeyError, config.set, "My_new_param", 100, strict=True)

def set_multiple(self):
config = SearchConfiguration()
self.assertIsNone(config["im_filepath"])
self.assertEqual(config["encode_psi_bytes"], -1)
self.assertEqual(config["encode_num_bytes"], -1)

d = {"im_filepath": "Here", "encode_psi_bytes": 2}
d = {"im_filepath": "Here", "encode_num_bytes": 2}
config.set_multiple(d)
self.assertEqual(config["im_filepath"], "Here")
self.assertEqual(config["encode_psi_bytes"], 2)
self.assertEqual(config["encode_num_bytes"], 2)

def test_from_dict(self):
d = {"im_filepath": "Here2", "num_obs": 5}
Expand Down
Loading

0 comments on commit 66165d5

Please sign in to comment.