Skip to content

Commit

Permalink
added starting pcdi generation
Browse files Browse the repository at this point in the history
  • Loading branch information
bfrosik committed Jul 9, 2021
1 parent 6b98b64 commit 9b0cbbf
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 105 deletions.
4 changes: 2 additions & 2 deletions cohere/include/bridge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class Bridge

int StartCalcWithGuess(int device, std::vector<float> data_buffer_r, std::vector<float> guess_buffer_r, std::vector<float> guess_buffer_i, std::vector<int> dim, const std::string & config);

int StartCalcWithGuessSupport(int device, std::vector<float> data_buffer_r, std::vector<float> guess_buffer_r, std::vector<float> guess_buffer_i, std::vector<int> support_buffer, std::vector<int> dim, const std::string & config);
int StartCalcWithGuessSupport(int device, std::vector<float> data_buffer_r, std::vector<float> guess_buffer_r, std::vector<float> guess_buffer_i, std::vector<int> support_buffer, std::vector<int> dim, const std::string & config, int start_coh);

int StartCalcWithGuessSupportCoh(int device, std::vector<float> data_buffer_r, std::vector<float> guess_buffer_r, std::vector<float> guess_buffer_i, std::vector<int> support_buffer, std::vector<int> dim, std::vector<float> coh_buffer, std::vector<int> coh_dim, const std::string & config);

int StartCalc(int device, std::vector<float> data_buffer_r, std::vector<int> dim, std::string const & config);
int StartCalc(int device, std::vector<float> data_buffer_r, std::vector<int> dim, std::string const & config, int start_coh);

std::vector<d_type> GetReciprocalR();
std::vector<d_type> GetReciprocalI();
Expand Down
3 changes: 2 additions & 1 deletion cohere/include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const int FIRST_RUN_ONLY = 1;
const int FOR_ALL_RUNS = 2;
const int MODIFIED_AFTER_FIRST = 3;
const int CUSTOM = 4;
const int TRIGGER = 5;

typedef struct flow_item_def {const char* item_name;
int type;
Expand Down Expand Up @@ -78,7 +79,7 @@ const flow_item_def flow_def[] = {
flow_item_def("shrink_wrap_trigger", MODIFIED_AFTER_FIRST, "ShrinkWrapTrigger"),
flow_item_def("phase_support_trigger", FIRST_RUN_ONLY, "PhaseTrigger"),
flow_item_def("to_reciprocal_space", NOT_TRIGGER, "ToReciprocal"),
flow_item_def("pcdi_trigger", MODIFIED_AFTER_FIRST, "PcdiTrigger"),
flow_item_def("pcdi_trigger", TRIGGER, "PcdiTrigger"),
flow_item_def("pcdi", CUSTOM, "Pcdi"),
flow_item_def("no_pcdi", CUSTOM, "NoPcdi"),
flow_item_def("garbage_trigger", NOT_TRIGGER, "Gc"),
Expand Down
4 changes: 2 additions & 2 deletions cohere/include/manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Manager
// The config parameter defines configuration file.
int StartCalc(int device, std::vector<d_type> data_buffer_r, std::vector<d_type> guess_buffer_r, std::vector<d_type> guess_buffer_i, std::vector<int> dim, const std::string & config);

int StartCalc(int device, std::vector<d_type> data_buffer_r, std::vector<d_type> guess_buffer_r, std::vector<d_type> guess_buffer_i, std::vector<int> support, std::vector<int> dim, const std::string & config);
int StartCalc(int device, std::vector<d_type> data_buffer_r, std::vector<d_type> guess_buffer_r, std::vector<d_type> guess_buffer_i, std::vector<int> support, std::vector<int> dim, const std::string & config, int start_coh);

int StartCalc(int device, std::vector<d_type> data_buffer_r, std::vector<d_type> guess_buffer_r, std::vector<d_type> guess_buffer_i, std::vector<int> support, std::vector<int> dim, std::vector<d_type> coh, std::vector<int> coh_dim, const std::string & config);

Expand All @@ -39,7 +39,7 @@ class Manager
// This method takes data, for the reconstruction algorithm. To perform the reconstruction the code will generate
// the guess parameter. The dim parameter conveys data dimensions, since the data is passed in a c-like buffer.
// The config parameter defines configuration file.
int StartCalc(int device, std::vector<d_type> data_buffer_r, std::vector<int> dim, std::string const & config);
int StartCalc(int device, std::vector<d_type> data_buffer_r, std::vector<int> dim, std::string const & config, int start_coh);

// This method starts calculations. The Manager uses workers to perform the calculations. The parameters define
// calculations type.
Expand Down
2 changes: 1 addition & 1 deletion cohere/include/parameters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Params

public:
// Constructor. Takes in configuration file, parses the configuration and sets the parameters accordingly.
Params(std::string const & config_file, std::vector<int> data_dim, bool first);
Params(std::string const & config_file, std::vector<int> data_dim, bool first, bool first_pcdi, bool pcdi);
~Params();

// Returns number of dimensions of the data array
Expand Down
8 changes: 4 additions & 4 deletions cohere/src_cpp/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ int Bridge::StartCalcWithGuess(int device, std::vector<float> data_buffer_r, std
return mgr->StartCalc(device, data_r, guess_r, guess_i, dim, config);
}

int Bridge::StartCalcWithGuessSupport(int device, std::vector<float> data_buffer_r, std::vector<float> guess_buffer_r, std::vector<float> guess_buffer_i, std::vector<int> support_buffer, std::vector<int> dim, const std::string & config)
int Bridge::StartCalcWithGuessSupport(int device, std::vector<float> data_buffer_r, std::vector<float> guess_buffer_r, std::vector<float> guess_buffer_i, std::vector<int> support_buffer, std::vector<int> dim, const std::string & config, int start_coh)
{
std::vector<d_type> data_r(data_buffer_r.begin(), data_buffer_r.end());
std::vector<d_type> guess_i(guess_buffer_i.begin(), guess_buffer_i.end());
std::vector<d_type> guess_r(guess_buffer_r.begin(), guess_buffer_r.end());
std::vector<int> support(support_buffer.begin(), support_buffer.end());
return mgr->StartCalc(device, data_r, guess_r, guess_i, support, dim, config);
return mgr->StartCalc(device, data_r, guess_r, guess_i, support, dim, config, start_coh);
}

int Bridge::StartCalcWithGuessSupportCoh(int device, std::vector<float> data_buffer_r, std::vector<float> guess_buffer_r, std::vector<float> guess_buffer_i, std::vector<int> support_buffer, std::vector<int> dim, std::vector<float> coh_buffer, std::vector<int> coh_dim, const std::string & config)
Expand All @@ -42,10 +42,10 @@ int Bridge::StartCalcWithGuessSupportCoh(int device, std::vector<float> data_buf
return mgr->StartCalc(device, data_r, guess_r, guess_i, support, dim, coh, coh_dim, config);
}

int Bridge::StartCalc(int device, std::vector<float> data_buffer_r, std::vector<int> dim, std::string const & config)
int Bridge::StartCalc(int device, std::vector<float> data_buffer_r, std::vector<int> dim, std::string const & config, int start_coh)
{
std::vector<d_type> data_r(data_buffer_r.begin(), data_buffer_r.end());
return mgr->StartCalc(device, data_r, dim, config);
return mgr->StartCalc(device, data_r, dim, config, start_coh);
}

std::vector<d_type> Bridge::GetImageR()
Expand Down
64 changes: 16 additions & 48 deletions cohere/src_cpp/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Manager::~Manager()
delete rec;
}

int Manager::StartCalc(int device, std::vector<d_type> data_buffer_r, std::vector<int> dim, std::string const & config)
int Manager::StartCalc(int device, std::vector<d_type> data_buffer_r, std::vector<int> dim, std::string const & config, int start_coh)
{
if(!( access( config.c_str(), F_OK ) == 0) )
{
Expand All @@ -53,7 +53,12 @@ int Manager::StartCalc(int device, std::vector<d_type> data_buffer_r, std::vecto
}

bool first = true;
Params * params = new Params(config, dim, first);
bool first_pcdi = false;
if (start_coh > 0)
{
first_pcdi = true;
}
Params * params = new Params(config, dim, first, first_pcdi, false);

dim4 af_dims = Utils::Int2Dim4(dim);
af::array real_d(af_dims, &data_buffer_r[0]);
Expand Down Expand Up @@ -114,55 +119,18 @@ int Manager::StartCalc(int device, std::vector<d_type> data_buffer_r, std::vecto

int Manager::StartCalc(int device, std::vector<d_type> data_buffer_r, std::vector<d_type> guess_buffer_r, std::vector<d_type> guess_buffer_i, std::vector<int> dim, const std::string & config)
{
bool first = false;
Params * params = new Params(config.c_str(), dim, first);

if (device >= 0)
{
try{
setDevice(device);
}
catch (...)
{
printf("no gpu with id %d, check configuration\n", device);
error_code = -2;
return error_code ;
}
info();
}

dim4 af_dims = Utils::Int2Dim4(dim);
af::array real_d(af_dims, &data_buffer_r[0]);
//saving abs(data)
af::array data = abs(real_d);

af::array real_g(af_dims, &guess_buffer_r[0]);
af::array imag_g(af_dims, &guess_buffer_i[0]);
af::array guess = complex(real_g, imag_g);

af::array null_array = array();

rec = new Reconstruction(data, guess, params, null_array, null_array);
rec->Init(first);
printf("initialized\n");

timer::start();
error_code = rec->Iterate();
if (error_code == 0)
{
printf("iterate function took %g seconds\n", timer::stop());
}
else
{
timer::stop();
}
return error_code;
printf("function not used, need to remove\n");
}

int Manager::StartCalc(int device, std::vector<d_type> data_buffer_r, std::vector<d_type> guess_buffer_r, std::vector<d_type> guess_buffer_i, std::vector<int> support_vector, std::vector<int> dim, const std::string & config)
int Manager::StartCalc(int device, std::vector<d_type> data_buffer_r, std::vector<d_type> guess_buffer_r, std::vector<d_type> guess_buffer_i, std::vector<int> support_vector, std::vector<int> dim, const std::string & config, int start_coh)
{
bool first = false;
Params * params = new Params(config.c_str(), dim, first);
bool first_pcdi = false;
if (start_coh > 0)
{
first_pcdi = true;
}
Params * params = new Params(config.c_str(), dim, first, first_pcdi, false);

if (device >= 0)
{
Expand Down Expand Up @@ -210,7 +178,7 @@ int Manager::StartCalc(int device, std::vector<d_type> data_buffer_r, std::vecto
int Manager::StartCalc(int device, std::vector<d_type> data_buffer_r, std::vector<d_type> guess_buffer_r, std::vector<d_type> guess_buffer_i, std::vector<int> support_vector, std::vector<int> dim, std::vector<d_type> coh_vector, std::vector<int> coh_dim, const std::string & config)
{
bool first = false;
Params * params = new Params(config.c_str(), dim, first);
Params * params = new Params(config.c_str(), dim, first, false, true);

if (device >= 0)
{
Expand Down
23 changes: 16 additions & 7 deletions cohere/src_cpp/parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ See LICENSE file.
#include "sstream"
#include "mutex"

Params::Params(std::string const & config_file, std::vector<int> data_dim, bool first)
Params::Params(std::string const & config_file, std::vector<int> data_dim, bool first, bool first_pcdi, bool pcdi)
{
is_resolution = false;
is_pcdi = false;
Expand Down Expand Up @@ -142,7 +142,7 @@ Params::Params(std::string const & config_file, std::vector<int> data_dim, bool
is_resolution = true;
}
// std::vector<int> pcdi_tr_iter;
if (parms.count("pcdi_trigger"))
if (parms.count("pcdi_trigger") && (first_pcdi || pcdi))
{
std::vector<std::string> tmp = ParseList(parms["pcdi_trigger"]);
for (int i = 0; i < int(tmp.size()); i++)
Expand All @@ -165,7 +165,7 @@ Params::Params(std::string const & config_file, std::vector<int> data_dim, bool
used_flow_seq.push_back(i);
}
else
{
{
if (type == CUSTOM)
{
if (strcmp(flow_item, "er") == 0)
Expand All @@ -174,20 +174,27 @@ Params::Params(std::string const & config_file, std::vector<int> data_dim, bool
}
else if (strcmp(flow_item, "hio") == 0)
{
used_flow_seq.push_back(i);
used_flow_seq.push_back(i);
}
else if (strcmp(flow_item, "no_pcdi") == 0)
{
if (!is_pcdi || first)
if (!is_pcdi || (is_pcdi && first_pcdi))
{
used_flow_seq.push_back(i);
used_flow_seq.push_back(i);
}
}
else if (is_pcdi)
{
used_flow_seq.push_back(i);
}
}
else if (type == TRIGGER)
{
if (is_pcdi && (strcmp(flow_item, "pcdi_trigger") == 0))
{
used_flow_seq.push_back(i);
}
}
else if (first)
{
if (type && parms.count(flow_item))
Expand Down Expand Up @@ -247,14 +254,16 @@ Params::Params(std::string const & config_file, std::vector<int> data_dim, bool
}
else if (strcmp(flow_item, "pcdi") == 0)
{
int start_pcdi = first ? pcdi_tr_iter[0] : 0;
int start_pcdi = first_pcdi ? pcdi_tr_iter[0] : 0;
for (int i = start_pcdi; i < number_iterations; i ++)
{
flow[offset + i] = 1;
}
}
else if (strcmp(flow_item, "no_pcdi") == 0)
{
// int start_pcdi = first_pcdi ? pcdi_tr_iter[0] : 0;
// int stop_pcdi = is_pcdi ? start_pcdi : number_iterations;
int stop_pcdi = is_pcdi ? pcdi_tr_iter[0] : number_iterations;
for (int i = 0; i < stop_pcdi; i ++)
{
Expand Down
5 changes: 2 additions & 3 deletions cohere/src_cpp/worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ int Reconstruction::Iterate()
(this->*iter_flow[current_iteration][i])();
}
}

if (aver_v.size() > 0)
{
af::array aver_a(ds_image.dims(), &aver_v[0]);
Expand Down Expand Up @@ -240,7 +239,7 @@ void Reconstruction::Pcdi()
current_error = GetNorm(abs(converged)(converged > 0)-iter_data(converged > 0))/GetNorm(iter_data);
state->RecordError(current_error);
rs_amplitudes *= ratio;
// printf("Pcdi %d\n", (uint)(getpid()));
// printf("Pcdi %d\n", (uint)(getpid()));
}

void Reconstruction::NoPcdi()
Expand All @@ -260,7 +259,7 @@ void Reconstruction::Gc()
void Reconstruction::SetPcdiPrevious()
{
partialCoherence->SetPrevious(abs(rs_amplitudes));
// printf("SetPcdiPrevious\n");
// printf("SetPcdiPrevious\n");
}

void Reconstruction::ToDirect()
Expand Down
2 changes: 1 addition & 1 deletion cohere/src_py/beamlines/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def visualize(self, image, support, coh, save_dir):
self.clear_direct_arrays()

if coh is not None:
coh = ut.get_zero_padded_centered(coh, image.shape)
coh = np.fft.fftshift(np.fft.fftn(np.fft.fftshift(coh)))
coh = ut.get_zero_padded_centered(coh, image.shape)
arrays = {"cohAmp": np.abs(coh), "cohPh": np.angle(coh)}
self.add_ds_arrays(arrays)
self.write_directspace(os.path.join(save_dir, 'coherence'))
Expand Down
7 changes: 3 additions & 4 deletions cohere/src_py/controller/fast_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
__all__ = ['fast_module_reconstruction',]


def fast_module_reconstruction(proc, device, conf, data, coh_dims, image=None, support=None, coherence=None):
def fast_module_reconstruction(proc, device, conf, data, coh_dims, image=None, support=None, coherence=None, first_pcdi=0):
"""
This function calls a bridge method corresponding to the requested processor type. The bridge method is an access
to the CFM (Calc Fast Module). When reconstruction is completed the function retrieves results from the CFM.
Expand Down Expand Up @@ -86,7 +86,7 @@ def fast_module_reconstruction(proc, device, conf, data, coh_dims, image=None, s
dims = data.shape[::-1]
data_l = data.flatten().tolist()
if image is None:
ec = fast_module.start_calc(device, data_l, dims, conf)
ec = fast_module.start_calc(device, data_l, dims, conf, first_pcdi)
# retry once if error code is -2 (NAN found), as the GPU might not load correctly on initial reconstruction
if ec == -2:
ec = fast_module.start_calc(device, data_l, dims, conf)
Expand All @@ -96,13 +96,12 @@ def fast_module_reconstruction(proc, device, conf, data, coh_dims, image=None, s
elif coherence is None:
image = image.flatten()
support = support.flatten()
ec = fast_module.start_calc_with_guess_support(device, data_l, image.real.tolist(), image.imag.tolist(), support.tolist(), dims, conf)
ec = fast_module.start_calc_with_guess_support(device, data_l, image.real.tolist(), image.imag.tolist(), support.tolist(), dims, conf, first_pcdi)
else:
image = image.flatten()
support = support.flatten()
coh_dims1 = (coh_dims[2], coh_dims[1], coh_dims[0])
coherence = coherence.flatten()

ec = fast_module.start_calc_with_guess_support_coh(device, data_l, image.real.tolist(), image.imag.tolist(), support.tolist(), dims, coherence.tolist(), coh_dims, conf)

if ec < 0:
Expand Down
Loading

0 comments on commit 9b0cbbf

Please sign in to comment.