repo_name
stringlengths
6
97
path
stringlengths
3
341
text
stringlengths
8
1.02M
davepallot/OSKAR
oskar/telescope/station/element/src/oskar_evaluate_geometric_dipole_pattern.c
<reponame>davepallot/OSKAR /* * Copyright (c) 2012-2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "telescope/station/element/oskar_evaluate_geometric_dipole_pattern.h" #include "telescope/station/element/oskar_evaluate_geometric_dipole_pattern_cuda.h" #include "telescope/station/element/oskar_evaluate_geometric_dipole_pattern_inline.h" #include "utility/oskar_device_utils.h" #include "math/oskar_cmath.h" #ifdef __cplusplus extern "C" { #endif /* Single precision. */ void oskar_evaluate_geometric_dipole_pattern_f(int num_points, const float* theta, const float* phi, int stride, float2* E_theta, float2* E_phi) { int i, i_out; for (i = 0; i < num_points; ++i) { i_out = i * stride; oskar_evaluate_geometric_dipole_pattern_inline_f(theta[i], phi[i], E_theta + i_out, E_phi + i_out); } } void oskar_evaluate_geometric_dipole_pattern_scalar_f(int num_points, const float* theta, const float* phi, int stride, float2* pattern) { float theta_, phi_, amp; float4c val; int i, i_out; for (i = 0; i < num_points; ++i) { /* Get source coordinates. */ theta_ = theta[i]; phi_ = phi[i]; /* Evaluate E_theta, E_phi for both X and Y dipoles. */ oskar_evaluate_geometric_dipole_pattern_inline_f(theta_, phi_, &val.a, &val.b); oskar_evaluate_geometric_dipole_pattern_inline_f(theta_, phi_ + ((float)M_PI) / 2.0f, &val.c, &val.d); /* Get sum of the diagonal of the autocorrelation matrix. */ amp = val.a.x * val.a.x + val.a.y * val.a.y + val.b.x * val.b.x + val.b.y * val.b.y + val.c.x * val.c.x + val.c.y * val.c.y + val.d.x * val.d.x + val.d.y * val.d.y; amp = sqrtf(0.5f * amp); /* Save amplitude. */ i_out = i * stride; pattern[i_out].x = amp; pattern[i_out].y = 0.0f; } } /* Double precision. */ void oskar_evaluate_geometric_dipole_pattern_d(int num_points, const double* theta, const double* phi, int stride, double2* E_theta, double2* E_phi) { int i, i_out; for (i = 0; i < num_points; ++i) { i_out = i * stride; oskar_evaluate_geometric_dipole_pattern_inline_d(theta[i], phi[i], E_theta + i_out, E_phi + i_out); } } void oskar_evaluate_geometric_dipole_pattern_scalar_d(int num_points, const double* theta, const double* phi, int stride, double2* pattern) { double theta_, phi_, amp; double4c val; int i, i_out; for (i = 0; i < num_points; ++i) { /* Get source coordinates. */ theta_ = theta[i]; phi_ = phi[i]; /* Evaluate E_theta, E_phi for both X and Y dipoles. */ oskar_evaluate_geometric_dipole_pattern_inline_d(theta_, phi_, &val.a, &val.b); oskar_evaluate_geometric_dipole_pattern_inline_d(theta_, phi_ + M_PI / 2.0, &val.c, &val.d); /* Get sum of the diagonal of the autocorrelation matrix. */ amp = val.a.x * val.a.x + val.a.y * val.a.y + val.b.x * val.b.x + val.b.y * val.b.y + val.c.x * val.c.x + val.c.y * val.c.y + val.d.x * val.d.x + val.d.y * val.d.y; amp = sqrt(0.5 * amp); /* Save amplitude. */ i_out = i * stride; pattern[i_out].x = amp; pattern[i_out].y = 0.0; } } /* Wrapper. */ void oskar_evaluate_geometric_dipole_pattern(oskar_Mem* pattern, int num_points, const oskar_Mem* theta, const oskar_Mem* phi, int offset, int stride, int* status) { int precision, type, location; /* Check if safe to proceed. */ if (*status) return; /* Get the meta-data. */ precision = oskar_mem_precision(pattern); type = oskar_mem_type(pattern); location = oskar_mem_location(pattern); /* Check that all arrays are co-located. */ if (oskar_mem_location(theta) != location || oskar_mem_location(phi) != location) { *status = OSKAR_ERR_LOCATION_MISMATCH; return; } /* Check that the pattern array is complex. */ if (!oskar_mem_is_complex(pattern)) { *status = OSKAR_ERR_BAD_DATA_TYPE; return; } /* Check that the types match. */ if (oskar_mem_type(theta) != precision || oskar_mem_type(phi) != precision) { *status = OSKAR_ERR_TYPE_MISMATCH; return; } /* Check the location. */ if (location == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA if (type == OSKAR_SINGLE_COMPLEX_MATRIX) { oskar_evaluate_geometric_dipole_pattern_cuda_f(num_points, oskar_mem_float_const(theta, status), oskar_mem_float_const(phi, status), stride, oskar_mem_float2(pattern, status) + offset, oskar_mem_float2(pattern, status) + offset + 1); } else if (type == OSKAR_DOUBLE_COMPLEX_MATRIX) { oskar_evaluate_geometric_dipole_pattern_cuda_d(num_points, oskar_mem_double_const(theta, status), oskar_mem_double_const(phi, status), stride, oskar_mem_double2(pattern, status) + offset, oskar_mem_double2(pattern, status) + offset + 1); } else if (type == OSKAR_SINGLE_COMPLEX) { oskar_evaluate_geometric_dipole_pattern_scalar_cuda_f(num_points, oskar_mem_float_const(theta, status), oskar_mem_float_const(phi, status), stride, oskar_mem_float2(pattern, status) + offset); } else if (type == OSKAR_DOUBLE_COMPLEX) { oskar_evaluate_geometric_dipole_pattern_scalar_cuda_d(num_points, oskar_mem_double_const(theta, status), oskar_mem_double_const(phi, status), stride, oskar_mem_double2(pattern, status) + offset); } oskar_device_check_error(status); #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } else if (location == OSKAR_CPU) { if (type == OSKAR_SINGLE_COMPLEX_MATRIX) { oskar_evaluate_geometric_dipole_pattern_f(num_points, oskar_mem_float_const(theta, status), oskar_mem_float_const(phi, status), stride, oskar_mem_float2(pattern, status) + offset, oskar_mem_float2(pattern, status) + offset + 1); } else if (type == OSKAR_DOUBLE_COMPLEX_MATRIX) { oskar_evaluate_geometric_dipole_pattern_d(num_points, oskar_mem_double_const(theta, status), oskar_mem_double_const(phi, status), stride, oskar_mem_double2(pattern, status) + offset, oskar_mem_double2(pattern, status) + offset + 1); } else if (type == OSKAR_SINGLE_COMPLEX) { oskar_evaluate_geometric_dipole_pattern_scalar_f(num_points, oskar_mem_float_const(theta, status), oskar_mem_float_const(phi, status), stride, oskar_mem_float2(pattern, status) + offset); } else if (type == OSKAR_DOUBLE_COMPLEX) { oskar_evaluate_geometric_dipole_pattern_scalar_d(num_points, oskar_mem_double_const(theta, status), oskar_mem_double_const(phi, status), stride, oskar_mem_double2(pattern, status) + offset); } } } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/imager/src/private_imager_generate_w_phase_screen.c
<reponame>davepallot/OSKAR /* * Copyright (c) 2016, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "imager/private_imager_generate_w_phase_screen.h" #include "imager/private_imager_generate_w_phase_screen_cuda.h" #include "math/oskar_cmath.h" #ifdef __cplusplus extern "C" { #endif void oskar_imager_generate_w_phase_screen(const int iw, const int conv_size, const int inner, const double sampling, const double w_scale, const oskar_Mem* taper_func, oskar_Mem* screen, int* status) { int iy; const double f = (2.0 * M_PI * iw * iw) / w_scale; const int inner_half = inner / 2; oskar_mem_clear_contents(screen, status); if (*status) return; if (oskar_mem_precision(screen) == OSKAR_SINGLE) { float* scr; const float* tp; scr = oskar_mem_float(screen, status); tp = oskar_mem_float_const(taper_func, status); if (oskar_mem_location(screen) == OSKAR_CPU) { for (iy = -inner_half; iy < inner_half; ++iy) { int ix, ind, offset; double l, m, msq, phase, rsq, taper, taper_x, taper_y; taper_y = tp[iy + inner_half]; m = sampling * (double)iy; msq = m*m; offset = (iy > -1 ? iy : (iy + conv_size)) * conv_size; for (ix = -inner_half; ix < inner_half; ++ix) { l = sampling * (double)ix; rsq = l*l + msq; if (rsq < 1.0) { taper_x = tp[ix + inner_half]; taper = taper_x * taper_y; ind = 2 * (offset + (ix > -1 ? ix : (ix + conv_size))); phase = f * (sqrt(1.0 - rsq) - 1.0); scr[ind] = taper * cos(phase); scr[ind + 1] = taper * sin(phase); } } } } else { #ifdef OSKAR_HAVE_CUDA oskar_imager_generate_w_phase_screen_cuda_f(iw, conv_size, inner, (float) sampling, (float) w_scale, tp, scr); #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } } else { double* scr; const double* tp; scr = oskar_mem_double(screen, status); tp = oskar_mem_double_const(taper_func, status); if (oskar_mem_location(screen) == OSKAR_CPU) { for (iy = -inner_half; iy < inner_half; ++iy) { int ix, ind, offset; double l, m, msq, phase, rsq, taper, taper_x, taper_y; taper_y = tp[iy + inner_half]; m = sampling * (double)iy; msq = m*m; offset = (iy > -1 ? iy : (iy + conv_size)) * conv_size; for (ix = -inner_half; ix < inner_half; ++ix) { l = sampling * (double)ix; rsq = l*l + msq; if (rsq < 1.0) { taper_x = tp[ix + inner_half]; taper = taper_x * taper_y; ind = 2 * (offset + (ix > -1 ? ix : (ix + conv_size))); phase = f * (sqrt(1.0 - rsq) - 1.0); scr[ind] = taper * cos(phase); scr[ind + 1] = taper * sin(phase); } } } } else { #ifdef OSKAR_HAVE_CUDA oskar_imager_generate_w_phase_screen_cuda_d(iw, conv_size, inner, sampling, w_scale, tp, scr); #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } } } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/interferometer/src/oskar_jones_join.c
<filename>oskar/interferometer/src/oskar_jones_join.c /* * Copyright (c) 2011-2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "interferometer/private_jones.h" #include "interferometer/oskar_jones.h" #ifdef __cplusplus extern "C" { #endif void oskar_jones_join(oskar_Jones* j3, oskar_Jones* j1, const oskar_Jones* j2, int* status) { int num_elements, n_sources1, n_sources2, n_sources3; int n_stations1, n_stations2, n_stations3; /* Check if safe to proceed. */ if (*status) return; /* Get the dimensions of the input data. */ if (!j3) j3 = j1; n_sources1 = j1->num_sources; n_sources2 = j2->num_sources; n_sources3 = j3->num_sources; n_stations1 = j1->num_stations; n_stations2 = j2->num_stations; n_stations3 = j3->num_stations; /* Check the data dimensions. */ if (n_sources1 != n_sources2 || n_sources1 != n_sources3) *status = OSKAR_ERR_DIMENSION_MISMATCH; if (n_stations1 != n_stations2 || n_stations1 != n_stations3) *status = OSKAR_ERR_DIMENSION_MISMATCH; /* Multiply the array elements. */ num_elements = n_sources1 * n_stations1; oskar_mem_multiply(j3->data, j1->data, j2->data, num_elements, status); } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/log/src/oskar_log_line.c
/* * Copyright (c) 2012-2013, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "log/private_log.h" #include "log/oskar_log.h" #include <stdarg.h> #ifdef __cplusplus extern "C" { #endif #ifdef OSKAR_OS_WIN /* http://stackoverflow.com/questions/15321493/how-should-i-pass-null-to-the-va-list-function-parameter */ static va_list do_create_empty_va_list(int i, ...) { va_list vl; va_start(vl, i); return vl; } static va_list create_empty_va_list() { return do_create_empty_va_list(0); } #endif void oskar_log_line(oskar_Log* log, char priority, char symbol) { char code = symbol; int depth = OSKAR_LOG_LINE; const char* prefix = 0; const char* format = 0; FILE* stream = (priority == 'E') ? stderr : stdout; #ifdef OSKAR_OS_WIN /* Print to stdout or stderr */ { va_list vl = create_empty_va_list(); oskar_log_write(log, stream, priority, code, depth, prefix, format, vl); va_end(vl); } /* Print to the log file */ if (log && log->file) { va_list vl = create_empty_va_list(); oskar_log_write(log, log->file, priority, code, depth, prefix, format, vl); va_end(vl); } #else va_list vl; /* Print to stdout or stderr */ oskar_log_write(log, stream, priority, code, depth, prefix, format, vl); /* Print to the log file */ if (log && log->file) { oskar_log_write(log, log->file, priority, code, depth, prefix, format, vl); } #endif } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/imager/src/private_imager_filter_time.c
/* * Copyright (c) 2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "imager/private_imager.h" #include "imager/private_imager_filter_time.h" #include <float.h> #ifdef __cplusplus extern "C" { #endif void oskar_imager_filter_time(const oskar_Imager* h, size_t* num_vis, oskar_Mem* uu, oskar_Mem* vv, oskar_Mem* ww, oskar_Mem* amp, oskar_Mem* weight, oskar_Mem* time_centroid, int* status) { size_t i, n; double t, range[2], *time_centroid_; /* Return immediately if filtering is not enabled. */ if ((h->time_min_utc <= 0.0 && h->time_max_utc <= 0.0) || !time_centroid || oskar_mem_length(time_centroid) == 0) return; if (*status) return; /* Get the range. */ range[0] = h->time_min_utc; range[1] = (h->time_max_utc <= 0.0) ? (double) FLT_MAX : h->time_max_utc; /* Get the number of input points, and set the number selected to zero. */ n = *num_vis; *num_vis = 0; /* Apply the time centroid filter. */ time_centroid_ = oskar_mem_double(time_centroid, status); if (h->imager_prec == OSKAR_DOUBLE) { double *uu_, *vv_, *ww_, *weight_; double2* amp_; uu_ = oskar_mem_double(uu, status); vv_ = oskar_mem_double(vv, status); ww_ = oskar_mem_double(ww, status); amp_ = oskar_mem_double2(amp, status); weight_ = oskar_mem_double(weight, status); for (i = 0; i < n; ++i) { t = time_centroid_[i]; if (t >= range[0] && t <= range[1]) { uu_[*num_vis] = uu_[i]; vv_[*num_vis] = vv_[i]; ww_[*num_vis] = ww_[i]; amp_[*num_vis] = amp_[i]; weight_[*num_vis] = weight_[i]; time_centroid_[*num_vis] = t; (*num_vis)++; } } } else { float *uu_, *vv_, *ww_, *weight_; float2* amp_; uu_ = oskar_mem_float(uu, status); vv_ = oskar_mem_float(vv, status); ww_ = oskar_mem_float(ww, status); amp_ = oskar_mem_float2(amp, status); weight_ = oskar_mem_float(weight, status); for (i = 0; i < n; ++i) { t = time_centroid_[i]; if (t >= range[0] && t <= range[1]) { uu_[*num_vis] = uu_[i]; vv_[*num_vis] = vv_[i]; ww_[*num_vis] = ww_[i]; amp_[*num_vis] = amp_[i]; weight_[*num_vis] = weight_[i]; time_centroid_[*num_vis] = t; (*num_vis)++; } } } } #ifdef __cplusplus } #endif
davepallot/OSKAR
gui/oskar_SettingsModel.h
/* * Copyright (c) 2015-2017, The University of Oxford * All rights reserved. * * This file is part of the OSKAR package. * Contact: oskar at oerc.ox.ac.uk * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_SETTINGS_MODEL_H_ #define OSKAR_SETTINGS_MODEL_H_ #include <QtCore/QAbstractItemModel> #include <QtCore/QDateTime> #include <QtCore/QString> #include <QtCore/QSortFilterProxyModel> class QModelIndex; class QStringList; class QVariant; namespace oskar { class SettingsTree; class SettingsNode; class SettingsModel : public QAbstractItemModel { Q_OBJECT public: enum ModelRole { KeyRole = Qt::UserRole, ValueRole, DefaultRole, TypeRole, OptionsRole, CheckExternalChangesRole, DisplayKeysRole, ItemTypeRole, RangeRole, ExtRangeRole, ResetGroupRole }; public: SettingsModel(SettingsTree* settings, QObject* parent = 0); virtual ~SettingsModel(); void beginReset(); void endReset(); int columnCount(const QModelIndex& parent = QModelIndex()) const; QVariant data(const QModelIndex& index, int role) const; Qt::ItemFlags flags(const QModelIndex& index) const; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const; void load_settings_file(const QString& filename = QString()); void save_settings_file(const QString& filename = QString()); QModelIndex parent(const QModelIndex& index) const; void refresh(); int rowCount(const QModelIndex& parent = QModelIndex()) const; bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole); signals: void fileReloaded(); private: const SettingsNode* get_node(const QModelIndex& index) const; void refresh(const QModelIndex& parent); void reset_group_(const SettingsNode* node); SettingsTree* settings_; QString filename_; QDateTime lastModified_; bool displayKey_; }; class SettingsModelFilter : public QSortFilterProxyModel { Q_OBJECT public: SettingsModelFilter(QObject* parent = 0); ~SettingsModelFilter(); QVariant data(const QModelIndex& index, int role) const; public slots: void setFilterRegExp(const QString& pattern); protected: bool filterAcceptsChildren(int sourceRow, const QModelIndex& sourceParent) const; bool filterAcceptsCurrentRow(const QModelIndex& idx) const; bool filterAcceptsCurrentRow(int sourceRow, const QModelIndex& sourceParent) const; virtual bool filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const; }; } /* namespace oskar */ #endif /* OSKAR_SETTINGS_MODEL_H_ */
davepallot/OSKAR
oskar/imager/src/private_imager_init_wproj.c
/* * Copyright (c) 2016-2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifdef OSKAR_HAVE_CUDA #include <cufft.h> #endif #include "imager/private_imager.h" #include "imager/oskar_imager.h" #include "imager/private_imager_composite_nearest_even.h" #include "imager/private_imager_generate_w_phase_screen.h" #include "imager/private_imager_init_wproj.h" #include "imager/oskar_grid_functions_spheroidal.h" #include "math/oskar_cmath.h" #include "math/oskar_fftpack_cfft.h" #include "math/oskar_fftpack_cfft_f.h" #include "utility/oskar_get_memory_usage.h" #include "utility/oskar_device_utils.h" #include <stdlib.h> #include <string.h> #include <stdio.h> #ifdef __cplusplus extern "C" { #endif #define MIN(a,b) ((a) < (b) ? (a) : (b)) #define MAX(a,b) ((a) > (b) ? (a) : (b)) #define SAVE_KERNELS 0 #if SAVE_KERNELS #include <fitsio.h> static void write_kernel_metadata(oskar_Imager* h, const char* fname, int* status) { fitsfile* f = 0; char *ttype[] = {"SUPPORT"}; char *tform[] = {"1J"}; /* 32-bit integer. */ char *tunit[] = {"\0"}; char extname[] = "W_KERNELS"; double fov_rad; int grid_size; fits_open_file(&f, fname, READWRITE, status); /* Write relevant imaging parameters as primary header keywords. */ fov_rad = h->fov_deg * M_PI / 180.0; grid_size = oskar_imager_plane_size(h); fits_write_key(f, TINT, "OVERSAMP", &h->oversample, "kernel oversample parameter", status); fits_write_key(f, TINT, "GRIDSIZE", &grid_size, "grid side length", status); fits_write_key(f, TINT, "IMSIZE", &h->image_size, "final image side length, in pixels", status); fits_write_key(f, TDOUBLE, "FOV", &fov_rad, "final image field of view, in radians", status); fits_write_key(f, TDOUBLE, "CELLSIZE", &h->cellsize_rad, "final image cell size, in radians", status); fits_write_key(f, TDOUBLE, "W_SCALE", &h->w_scale, "w_scale parameter", status); /* Write kernel support sizes as a binary table extension. */ fits_create_tbl(f, BINARY_TBL, h->num_w_planes, 1, ttype, tform, tunit, extname, status); fits_write_col(f, TINT, 1, 1, 1, h->num_w_planes, oskar_mem_int(h->w_support, status), status); fits_close_file(f, status); } #endif static void compact_kernels(const int num_w_planes, const int* support, const int oversample, const int conv_size_half, const oskar_Mem* kernels_in, oskar_Mem* kernels_out, int* compacted_kernel_start, int* status); /* * W-kernel generation is based on CASA implementation * in code/synthesis/TransformMachines/WPConvFunc.cc */ void oskar_imager_init_wproj(oskar_Imager* h, int* status) { size_t max_mem_bytes, max_bytes_per_plane, element_size, copy_len; int i, iw, ix, iy, *supp, new_conv_size, oversample, prec; int conv_size, conv_size_half, inner, nearest; double l_max, max_conv_size, max_uvw, max_val, sampling, sum; double *maxes; #ifdef OSKAR_HAVE_CUDA cufftHandle cufft_plan = 0; #endif oskar_Mem *screen = 0, *screen_gpu = 0, *screen_ptr = 0; oskar_Mem *taper = 0, *taper_gpu = 0, *taper_ptr = 0; oskar_Mem *wsave = 0, *work = 0; char *ptr_out, *ptr_in, *fname = 0; if (*status) return; /* Get GCF padding oversample factor and imager precision. */ oversample = h->oversample; prec = h->imager_prec; /* Calculate required number of w-planes if not set. */ if (h->ww_max > 0.0) { double ww_mid; max_uvw = 1.05 * h->ww_max; ww_mid = 0.5 * (h->ww_min + h->ww_max); if (h->ww_rms > ww_mid) max_uvw *= h->ww_rms / ww_mid; } else { max_uvw = 0.25 / fabs(h->cellsize_rad); } if (h->num_w_planes < 1) h->num_w_planes = (int)(max_uvw * fabs(sin(h->cellsize_rad * h->image_size / 2.0))); if (h->num_w_planes < 16) h->num_w_planes = 16; /* Calculate convolution kernel size. */ h->w_scale = pow(h->num_w_planes - 1, 2.0) / max_uvw; max_mem_bytes = oskar_get_total_physical_memory(); max_bytes_per_plane = 64 * 1024 * 1024; /* 64 MB per plane */ max_mem_bytes = MIN(max_mem_bytes, max_bytes_per_plane * h->num_w_planes); max_conv_size = sqrt(max_mem_bytes / (16.0 * h->num_w_planes)); nearest = oskar_imager_composite_nearest_even( 2 * (int)(max_conv_size / 2.0), 0, 0); conv_size = MIN((int)(h->image_size * h->image_padding), nearest); conv_size_half = conv_size / 2 - 1; h->conv_size_half = conv_size_half; /* Allocate kernels and support array. */ oskar_mem_free(h->w_kernels, status); oskar_mem_free(h->w_support, status); oskar_mem_free(h->w_kernels_compact, status); oskar_mem_free(h->w_kernel_start, status); h->w_support = oskar_mem_create(OSKAR_INT, OSKAR_CPU, h->num_w_planes, status); h->w_kernel_start = oskar_mem_create(OSKAR_INT, OSKAR_CPU, h->num_w_planes, status); h->w_kernels = oskar_mem_create(prec | OSKAR_COMPLEX, OSKAR_CPU, ((size_t) h->num_w_planes) * ((size_t) conv_size_half) * ((size_t) conv_size_half), status); h->w_kernels_compact = oskar_mem_create(prec | OSKAR_COMPLEX, OSKAR_CPU, 0, status); supp = oskar_mem_int(h->w_support, status); element_size = oskar_mem_element_size(oskar_mem_type(h->w_kernels)); if (*status) return; /* Get size of inner region of kernel and padded grid size. */ inner = conv_size / oversample; l_max = sin(0.5 * h->fov_deg * M_PI/180.0); sampling = (2.0 * l_max * oversample) / h->image_size; sampling *= ((double) oskar_imager_plane_size(h)) / ((double) conv_size); /* Create scratch arrays and FFT plan for the phase screens. */ screen = oskar_mem_create(prec | OSKAR_COMPLEX, OSKAR_CPU, conv_size * conv_size, status); screen_ptr = screen; #ifdef OSKAR_HAVE_CUDA if (h->generate_w_kernels_on_gpu && h->num_gpus > 0) { oskar_device_set(h->gpu_ids[0], status); screen_gpu = oskar_mem_create(prec | OSKAR_COMPLEX, OSKAR_GPU, conv_size * conv_size, status); screen_ptr = screen_gpu; if (prec == OSKAR_DOUBLE) cufftPlan2d(&cufft_plan, conv_size, conv_size, CUFFT_Z2Z); else cufftPlan2d(&cufft_plan, conv_size, conv_size, CUFFT_C2C); } else #endif { int len_save = 4 * conv_size + 2 * (int)(log((double)conv_size) / log(2.0)) + 8; wsave = oskar_mem_create(prec, OSKAR_CPU, len_save, status); work = oskar_mem_create(prec, OSKAR_CPU, 2 * conv_size * conv_size, status); if (prec == OSKAR_DOUBLE) oskar_fftpack_cfft2i(conv_size, conv_size, oskar_mem_double(wsave, status)); else oskar_fftpack_cfft2i_f(conv_size, conv_size, oskar_mem_float(wsave, status)); } /* Generate 1D spheroidal tapering function to cover the inner region. */ taper = oskar_mem_create(prec, OSKAR_CPU, inner, status); taper_ptr = taper; if (prec == OSKAR_DOUBLE) { double* t = oskar_mem_double(taper, status); for (i = 0; i < inner; ++i) { double nu; nu = (i - (inner / 2)) / ((double)(inner / 2)); t[i] = oskar_grid_function_spheroidal(fabs(nu)); } } else { float* t = oskar_mem_float(taper, status); for (i = 0; i < inner; ++i) { double nu; nu = (i - (inner / 2)) / ((double)(inner / 2)); t[i] = oskar_grid_function_spheroidal(fabs(nu)); } } #ifdef OSKAR_HAVE_CUDA if (h->generate_w_kernels_on_gpu && h->num_gpus > 0) { taper_gpu = oskar_mem_create_copy(taper, OSKAR_GPU, status); taper_ptr = taper_gpu; } #endif /* Evaluate kernels. */ ptr_in = oskar_mem_char(screen); copy_len = element_size * conv_size_half; maxes = (double*) calloc(h->num_w_planes, sizeof(double)); for (iw = 0; iw < h->num_w_planes; ++iw) { size_t in = 0, out = 0, offset; /* Generate the tapered phase screen. */ oskar_imager_generate_w_phase_screen(iw, conv_size, inner, sampling, h->w_scale, taper_ptr, screen_ptr, status); if (*status) break; /* Perform the FFT to get the kernel. No shifts are required. */ #ifdef OSKAR_HAVE_CUDA if (h->generate_w_kernels_on_gpu && h->num_gpus > 0) { if (oskar_mem_precision(screen) == OSKAR_DOUBLE) cufftExecZ2Z(cufft_plan, oskar_mem_void(screen_ptr), oskar_mem_void(screen_ptr), CUFFT_FORWARD); else cufftExecC2C(cufft_plan, oskar_mem_void(screen_ptr), oskar_mem_void(screen_ptr), CUFFT_FORWARD); oskar_mem_copy(screen, screen_ptr, status); } else #endif { if (oskar_mem_precision(screen_ptr) == OSKAR_DOUBLE) oskar_fftpack_cfft2f(conv_size, conv_size, conv_size, oskar_mem_double(screen_ptr, status), oskar_mem_double(wsave, status), oskar_mem_double(work, status)); else oskar_fftpack_cfft2f_f(conv_size, conv_size, conv_size, oskar_mem_float(screen_ptr, status), oskar_mem_float(wsave, status), oskar_mem_float(work, status)); } if (*status) break; /* Get the maximum (from the first element). */ if (oskar_mem_precision(screen) == OSKAR_DOUBLE) { const double* t = (const double*) oskar_mem_void_const(screen); maxes[iw] = sqrt(t[0]*t[0] + t[1]*t[1]); } else { const float* t = (const float*) oskar_mem_void_const(screen); maxes[iw] = sqrt(t[0]*t[0] + t[1]*t[1]); } /* Save only the first quarter of the kernel; the rest is redundant. */ offset = iw * conv_size_half * conv_size_half * element_size; ptr_out = oskar_mem_char(h->w_kernels) + offset; for (iy = 0; iy < conv_size_half; ++iy) { memcpy(ptr_out + out, ptr_in + in, copy_len); in += conv_size * element_size; out += copy_len; } } /* Clean up. */ #ifdef OSKAR_HAVE_CUDA cufftDestroy(cufft_plan); #endif oskar_mem_free(screen, status); oskar_mem_free(screen_gpu, status); oskar_mem_free(taper, status); oskar_mem_free(taper_gpu, status); oskar_mem_free(wsave, status); oskar_mem_free(work, status); /* Normalise each plane by the maximum. */ if (*status) return; max_val = -INT_MAX; for (iw = 0; iw < h->num_w_planes; ++iw) max_val = MAX(max_val, maxes[iw]); oskar_mem_scale_real(h->w_kernels, 1.0 / max_val, status); free(maxes); /* Find the support size of each kernel by stepping in from the edge. */ for (iw = 0; iw < h->num_w_planes; ++iw) { int trial = 0, found = 0, plane_offset, ind1, ind2; double v1, v2; if (*status) break; plane_offset = conv_size_half * conv_size_half * iw; if (oskar_mem_precision(h->w_kernels) == OSKAR_DOUBLE) { const double *restrict p = oskar_mem_double_const( h->w_kernels, status); for (trial = conv_size_half - 1; trial > 0; trial--) { ind1 = 2 * (trial * conv_size_half + plane_offset); ind2 = 2 * (trial + plane_offset); v1 = sqrt(p[ind1]*p[ind1] + p[ind1+1]*p[ind1+1]); v2 = sqrt(p[ind2]*p[ind2] + p[ind2+1]*p[ind2+1]); if ((v1 > 1e-3) || (v2 > 1e-3)) { found = 1; break; } } } else { const float *restrict p = oskar_mem_float_const( h->w_kernels, status); for (trial = conv_size_half - 1; trial > 0; trial--) { ind1 = 2 * (trial * conv_size_half + plane_offset); ind2 = 2 * (trial + plane_offset); v1 = sqrt(p[ind1]*p[ind1] + p[ind1+1]*p[ind1+1]); v2 = sqrt(p[ind2]*p[ind2] + p[ind2+1]*p[ind2+1]); if ((v1 > 1e-3) || (v2 > 1e-3)) { found = 1; break; } } } if (found) { supp[iw] = 1 + (int)(0.5 + (double)trial / (double)oversample); if (supp[iw] * oversample * 2 >= conv_size) supp[iw] = conv_size / 2 / oversample - 1; } } if (*status) return; /* Compact the kernels if we can. */ max_val = -INT_MAX; for (iw = 0; iw < h->num_w_planes; ++iw) max_val = MAX(max_val, supp[iw]); new_conv_size = 2 * (max_val + 2) * oversample; if (new_conv_size < conv_size) { char *ptr; int new_conv_size_half; size_t in = 0, out = 0; ptr = oskar_mem_char(h->w_kernels); new_conv_size_half = new_conv_size / 2 - 2; copy_len = element_size * new_conv_size_half; for (iw = 0; iw < h->num_w_planes; ++iw) { in = iw * conv_size_half * conv_size_half * element_size; for (iy = 0; iy < new_conv_size_half; ++iy) { /* Use memmove() rather than memcpy() to allow for overlap. */ memmove(ptr + out, ptr + in, copy_len); in += conv_size_half * element_size; out += copy_len; } } conv_size = new_conv_size; h->conv_size_half = conv_size_half = new_conv_size_half; oskar_mem_realloc(h->w_kernels, ((size_t) h->num_w_planes) * ((size_t) new_conv_size_half) * ((size_t) new_conv_size_half), status); } if (*status) return; #if 0 /* Print kernel support sizes. */ for (iw = 0; iw < h->num_w_planes; ++iw) { int *supp = oskar_mem_int(h->w_support, status); printf("Plane %d, support: %d\n", iw, supp[iw] * oversample); } #endif /* Normalise so that kernel 0 sums to 1, * when jumping in steps of oversample. */ sum = 0.0; /* Real part only. */ if (oskar_mem_precision(h->w_kernels) == OSKAR_DOUBLE) { const double *restrict p = oskar_mem_double_const(h->w_kernels, status); for (iy = -supp[0]; iy <= supp[0]; ++iy) for (ix = -supp[0]; ix <= supp[0]; ++ix) sum += p[2 * (abs(ix) * oversample + conv_size_half * (abs(iy) * oversample))]; } else { const float *restrict p = oskar_mem_float_const(h->w_kernels, status); for (iy = -supp[0]; iy <= supp[0]; ++iy) for (ix = -supp[0]; ix <= supp[0]; ++ix) sum += p[2 * (abs(ix) * oversample + conv_size_half * (abs(iy) * oversample))]; } oskar_mem_scale_real(h->w_kernels, 1.0 / sum, status); #if SAVE_KERNELS /* Save kernels to a FITS file if necessary. */ fname = (char*) calloc(20 + (h->input_root ? strlen(h->input_root) : 0), 1); sprintf(fname, "%s_KERNELS", h->input_root ? h->input_root : ""); oskar_mem_write_fits_cube(h->w_kernels, fname, conv_size_half, conv_size_half, h->num_w_planes, -1, status); /* Write kernel metadata. */ sprintf(fname, "%s_KERNELS_REAL.fits", h->input_root ? h->input_root : ""); write_kernel_metadata(h, fname, status); sprintf(fname, "%s_KERNELS_IMAG.fits", h->input_root ? h->input_root : ""); write_kernel_metadata(h, fname, status); #endif free(fname); /* Compact and rearrange the kernels. */ compact_kernels(h->num_w_planes, supp, oversample, conv_size_half, h->w_kernels, h->w_kernels_compact, oskar_mem_int(h->w_kernel_start, status), status); } static void compact_kernels(const int num_w_planes, const int* support, const int oversample, const int conv_size_half, const oskar_Mem* kernels_in, oskar_Mem* kernels_out, int* compacted_kernel_start, int* status) { int compacted_size = 0, w, j, k, off_u, off_v; float2* out_f; double2* out_d; const float2* in_f = (const float2*) oskar_mem_void_const(kernels_in); const double2* in_d = (const double2*) oskar_mem_void_const(kernels_in); const int oversample_h = oversample / 2; const int prec = oskar_mem_precision(kernels_in); /* Inside each kernel, we only access elements at locations * id = abs(off + i * oversample) * where * off \in [-oversample/2, oversample/2] * and * i = -w_support, ..., -1, 0, 1, ..., w_support * * This means we access locations between * id = 0 and id = oversample/2 + w_support * oversample * * The size of each compacted convolution kernel is therefore * (oversample/2 + w_support * oversample + 1)^2 * * Allocate enough memory for the compacted kernels. */ for (w = 0; w < num_w_planes; w++) { int size = oversample_h + support[w] * oversample + 1; size = size * size; compacted_kernel_start[w] = compacted_size; compacted_size += size; } oskar_mem_realloc(kernels_out, (size_t) compacted_size, status); out_f = (float2*) oskar_mem_void(kernels_out); out_d = (double2*) oskar_mem_void(kernels_out); for (w = 0; w < num_w_planes; w++) { const int w_support = support[w]; const int start_in = w * conv_size_half * conv_size_half; const int start_out = compacted_kernel_start[w]; const int size = oversample_h + w_support * oversample + 1; for (off_v = -oversample_h + 1; off_v <= oversample_h; off_v++) { for (j = 0; j <= w_support; j++) { /* Use the original layout in V/Y dimension. */ const int iy = abs(off_v + j * oversample); for (off_u = -oversample_h + 1; off_u <= oversample_h; off_u++) { for (k = 0; k <= w_support; k++) { int abs_off_u, i_in, i_out, my_k, my_idx, off; const int ix = abs(off_u + k * oversample); /* We want linear stride in the kernel's U direction. * We do this as follows ( idx(x) = (x ? 1 : 0) below) * * If off_u = -oversample/2 * idx = w_support - k + ind(k >= 1) * If 0 >= off_u > -oversample/2 * idx = w_support - k * If off_u > 0 * idx = idx(-k) - symmetry in k * * We now need to decide how to store these things. * For 0 > off_u > -oversample/2 * store the row at |off_u - 1| * row_length = |off_u - 1| * (2w_support + 1) * For off_u = 0 * store the row at (oversample/2 - 1) * prev_row_lengths = (oversample/2 - 1) * (2w_support + 1) * For off_u = -oversample/2 * store the row at the end of the data for off_u * = (oversample/2 - 1) * (2w_support + 1) + (w_support + 1) */ abs_off_u = abs(off_u); if (abs_off_u == 0) abs_off_u = oversample_h; off = (abs_off_u - 1) * (2 * w_support + 1); if (abs(off_u) == oversample_h) off += w_support + 1; my_k = (off_u <= 0) ? k : -k; if (off_u == 0) my_idx = w_support - abs(k); else if (abs_off_u == oversample_h) my_idx = w_support - abs(k) + (my_k >= 1 ? 1 : 0); else my_idx = w_support + my_k; i_in = start_in + iy * conv_size_half + ix; i_out = start_out + iy * size + (off + my_idx); if (prec == OSKAR_SINGLE) out_f[i_out] = in_f[i_in]; else out_d[i_out] = in_d[i_in]; } } } } #if 0 if (w == 0) { oskar_Mem* alias = oskar_mem_create_alias(kernels_out, start_out, size * size, status); oskar_mem_write_fits_cube(alias, "kernel_compacted", size, size, 1, 0, status); printf("At w = %d, w_support = %d\n", w, w_support); } #endif } #if 0 FILE* fhan = fopen("compacted_kernels.txt", "w"); oskar_mem_save_ascii(fhan, 1, compacted_size, status, kernels_out); fclose(fhan); #endif } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/telescope/station/src/oskar_evaluate_element_weights_dft.c
/* * Copyright (c) 2012-2018, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "telescope/station/oskar_evaluate_element_weights_dft.h" #include "telescope/station/oskar_evaluate_element_weights_dft_cuda.h" #include "utility/oskar_cl_utils.h" #include "utility/oskar_device_utils.h" #include <math.h> #ifdef __cplusplus extern "C" { #endif /* Single precision. */ void oskar_evaluate_element_weights_dft_f(const int num_elements, const float* x, const float* y, const float* z, const float wavenumber, const float x_beam, const float y_beam, const float z_beam, float2* weights) { int i; for (i = 0; i < num_elements; ++i) { float phase; float2 weight; phase = wavenumber * (x[i] * x_beam + y[i] * y_beam + z[i] * z_beam); weight.x = cosf(-phase); weight.y = sinf(-phase); weights[i] = weight; } } /* Double precision. */ void oskar_evaluate_element_weights_dft_d(const int num_elements, const double* x, const double* y, const double* z, const double wavenumber, const double x_beam, const double y_beam, const double z_beam, double2* weights) { int i; for (i = 0; i < num_elements; ++i) { double phase; double2 weight; phase = wavenumber * (x[i] * x_beam + y[i] * y_beam + z[i] * z_beam); weight.x = cos(-phase); weight.y = sin(-phase); weights[i] = weight; } } /* Wrapper. */ void oskar_evaluate_element_weights_dft(int num_elements, const oskar_Mem* x, const oskar_Mem* y, const oskar_Mem* z, double wavenumber, double x_beam, double y_beam, double z_beam, oskar_Mem* weights, int* status) { int type, location; /* Check if safe to proceed. */ if (*status) return; /* Check array dimensions are OK. */ if ((int)oskar_mem_length(weights) < num_elements || (int)oskar_mem_length(x) < num_elements || (int)oskar_mem_length(y) < num_elements || (int)oskar_mem_length(z) < num_elements) { *status = OSKAR_ERR_DIMENSION_MISMATCH; return; } /* Check for location mismatch. */ location = oskar_mem_location(weights); if (oskar_mem_location(x) != location || oskar_mem_location(y) != location || oskar_mem_location(z) != location) { *status = OSKAR_ERR_LOCATION_MISMATCH; return; } /* Check types. */ type = oskar_mem_precision(weights); if (!oskar_mem_is_complex(weights) || oskar_mem_is_matrix(weights)) { *status = OSKAR_ERR_BAD_DATA_TYPE; return; } if (oskar_mem_type(x) != type || oskar_mem_type(y) != type || oskar_mem_type(z) != type) { *status = OSKAR_ERR_TYPE_MISMATCH; return; } /* Generate DFT weights. */ if (location == OSKAR_CPU) { if (type == OSKAR_DOUBLE) oskar_evaluate_element_weights_dft_d(num_elements, oskar_mem_double_const(x, status), oskar_mem_double_const(y, status), oskar_mem_double_const(z, status), wavenumber, x_beam, y_beam, z_beam, oskar_mem_double2(weights, status)); else if (type == OSKAR_SINGLE) oskar_evaluate_element_weights_dft_f(num_elements, oskar_mem_float_const(x, status), oskar_mem_float_const(y, status), oskar_mem_float_const(z, status), wavenumber, x_beam, y_beam, z_beam, oskar_mem_float2(weights, status)); else *status = OSKAR_ERR_BAD_DATA_TYPE; } else if (location == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA if (type == OSKAR_DOUBLE) oskar_evaluate_element_weights_dft_cuda_d(num_elements, oskar_mem_double_const(x, status), oskar_mem_double_const(y, status), oskar_mem_double_const(z, status), wavenumber, x_beam, y_beam, z_beam, oskar_mem_double2(weights, status)); else if (type == OSKAR_SINGLE) oskar_evaluate_element_weights_dft_cuda_f(num_elements, oskar_mem_float_const(x, status), oskar_mem_float_const(y, status), oskar_mem_float_const(z, status), wavenumber, x_beam, y_beam, z_beam, oskar_mem_float2(weights, status)); else *status = OSKAR_ERR_BAD_DATA_TYPE; oskar_device_check_error(status); #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } else if (location & OSKAR_CL) { #ifdef OSKAR_HAVE_OPENCL cl_device_type dev_type; cl_event event; cl_kernel k = 0; cl_int is_gpu, error, num; cl_uint arg = 0; size_t global_size, local_size; clGetDeviceInfo(oskar_cl_device_id(), CL_DEVICE_TYPE, sizeof(cl_device_type), &dev_type, NULL); is_gpu = dev_type & CL_DEVICE_TYPE_GPU; if (type == OSKAR_DOUBLE) k = oskar_cl_kernel("evaluate_element_weights_dft_double"); else if (type == OSKAR_SINGLE) k = oskar_cl_kernel("evaluate_element_weights_dft_float"); else { *status = OSKAR_ERR_BAD_DATA_TYPE; return; } if (!k) { *status = OSKAR_ERR_FUNCTION_NOT_AVAILABLE; return; } /* Set kernel arguments. */ num = (cl_int) num_elements; error = clSetKernelArg(k, arg++, sizeof(cl_int), &num); error |= clSetKernelArg(k, arg++, sizeof(cl_mem), oskar_mem_cl_buffer_const(x, status)); error |= clSetKernelArg(k, arg++, sizeof(cl_mem), oskar_mem_cl_buffer_const(y, status)); error |= clSetKernelArg(k, arg++, sizeof(cl_mem), oskar_mem_cl_buffer_const(z, status)); if (type == OSKAR_SINGLE) { const cl_float w = (cl_float) wavenumber; const cl_float x1 = (cl_float) x_beam; const cl_float y1 = (cl_float) y_beam; const cl_float z1 = (cl_float) z_beam; error |= clSetKernelArg(k, arg++, sizeof(cl_float), &w); error |= clSetKernelArg(k, arg++, sizeof(cl_float), &x1); error |= clSetKernelArg(k, arg++, sizeof(cl_float), &y1); error |= clSetKernelArg(k, arg++, sizeof(cl_float), &z1); } else if (type == OSKAR_DOUBLE) { const cl_double w = (cl_double) wavenumber; const cl_double x1 = (cl_double) x_beam; const cl_double y1 = (cl_double) y_beam; const cl_double z1 = (cl_double) z_beam; error |= clSetKernelArg(k, arg++, sizeof(cl_double), &w); error |= clSetKernelArg(k, arg++, sizeof(cl_double), &x1); error |= clSetKernelArg(k, arg++, sizeof(cl_double), &y1); error |= clSetKernelArg(k, arg++, sizeof(cl_double), &z1); } error |= clSetKernelArg(k, arg++, sizeof(cl_mem), oskar_mem_cl_buffer(weights, status)); if (error != CL_SUCCESS) { *status = OSKAR_ERR_INVALID_ARGUMENT; return; } /* Launch kernel on current command queue. */ local_size = is_gpu ? 256 : 128; global_size = ((num + local_size - 1) / local_size) * local_size; error = clEnqueueNDRangeKernel(oskar_cl_command_queue(), k, 1, NULL, &global_size, &local_size, 0, NULL, &event); if (error != CL_SUCCESS) *status = OSKAR_ERR_KERNEL_LAUNCH_FAILURE; #else *status = OSKAR_ERR_OPENCL_NOT_AVAILABLE; #endif } else *status = OSKAR_ERR_BAD_LOCATION; } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/telescope/station/element/oskar_element_load_cst.h
/* * Copyright (c) 2012-2014, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_ELEMENT_LOAD_CST_H_ #define OSKAR_ELEMENT_LOAD_CST_H_ /** * @file oskar_element_load_cst.h */ #include <oskar_global.h> #include <log/oskar_log.h> #ifdef __cplusplus extern "C" { #endif /** * @brief * Loads an antenna pattern from an ASCII text file produced by CST. * * @details * This function loads antenna pattern data from a text file and fills the * provided data structure. * * The data file must contain eight columns, in the following order: * - <theta, deg> * - <phi, deg> * - <abs dir> * - <abs theta> * - <phase theta, deg> * - <abs phi> * - <phase phi, deg> * - <ax. ratio> * * This is the format exported by the CST (Computer Simulation Technology) * package. * * Amplitude values in dBi are detected, and converted to linear format * on loading. * * @param[in,out] data Pointer to element model data structure to fill. * @param[in,out] log Pointer to log structure to use. * @param[in] port Port number: 1 for X dipole, 2 for Y dipole. * @param[in] freq_hz Frequency at which element data applies, in Hz. * @param[in] filename Data file name. * @param[in] closeness Target average fractional error required (<< 1). * @param[in] closeness_inc Average fractional error factor increase (> 1). * @param[in] ignore_at_poles If set, ignore data at theta = 0 and theta = 180. * @param[in] ignore_below_horizon If set, ignore data at theta > 90 deg. * @param[in,out] status Status return code. */ OSKAR_EXPORT void oskar_element_load_cst(oskar_Element* data, oskar_Log* log, int port, double freq_hz, const char* filename, double closeness, double closeness_inc, int ignore_at_poles, int ignore_below_horizon, int* status); #ifdef __cplusplus } #endif #endif /* OSKAR_ELEMENT_LOAD_CST_H_ */
davepallot/OSKAR
oskar/utility/src/oskar_cuda_info_log.c
<filename>oskar/utility/src/oskar_cuda_info_log.c<gh_stars>1-10 /* * Copyright (c) 2012-2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "utility/private_cuda_info.h" #include "utility/oskar_cuda_info_log.h" #include "log/oskar_log.h" #ifdef __cplusplus extern "C" { #endif void oskar_cuda_info_log(oskar_Log* log, const oskar_CudaInfo* info) { int i; char p = 'M'; /* Log entry priority = message */ oskar_log_section(log, p, "CUDA System Information"); /* Print driver and runtime version. */ oskar_log_value(log, p, 0, "CUDA driver version", "%d.%d", info->driver_version / 1000, (info->driver_version % 100) / 10); oskar_log_value(log, p, 0, "CUDA runtime version", "%d.%d", info->runtime_version / 1000, (info->runtime_version % 100) / 10); oskar_log_value(log, p, 0, "Number of CUDA devices detected", "%d", info->num_devices); /* Print device array. */ for (i = 0; i < info->num_devices; ++i) { oskar_log_message(log, p, 0, "Device %d (%s):", i, info->device[i].name); oskar_log_value(log, p, 1, "Compute capability", "%d.%d", info->device[i].compute.capability.major, info->device[i].compute.capability.minor); oskar_log_value(log, p, 1, "Supports double precision", "%s", info->device[i].supports_double ? "true" : "false"); oskar_log_value(log, p, 1, "Global memory (MiB)", "%.1f", info->device[i].global_memory_size / 1024.0); oskar_log_value(log, p, 1, "Free global memory (MiB)", "%.1f", info->device[i].free_memory / 1024.0); oskar_log_value(log, p, 1, "Number of multiprocessors", "%d", info->device[i].num_multiprocessors); oskar_log_value(log, p, 1, "Number of CUDA cores", "%d", info->device[i].num_cores); oskar_log_value(log, p, 1, "GPU clock speed (MHz)", "%.0f", info->device[i].gpu_clock / 1000.0); oskar_log_value(log, p, 1, "Memory clock speed (MHz)", "%.0f", info->device[i].memory_clock / 1000.0); oskar_log_value(log, p, 1, "Memory bus width", "%d-bit", info->device[i].memory_bus_width); oskar_log_value(log, p, 1, "Level-2 cache size (kiB)", "%.0f", info->device[i].level_2_cache_size / 1024.0); oskar_log_value(log, p, 1, "Shared memory size (kiB)", "%.0f", info->device[i].shared_memory_size / 1024.0); oskar_log_value(log, p, 1, "Registers per block", "%d", info->device[i].num_registers); oskar_log_value(log, p, 1, "Warp size", "%d", info->device[i].warp_size); oskar_log_value(log, p, 1, "Max threads per block", "%d", info->device[i].max_threads_per_block); oskar_log_value(log, p, 1, "Max dimensions of a thread block", "(%d x %d x %d)", info->device[i].max_threads_dim[0], info->device[i].max_threads_dim[1], info->device[i].max_threads_dim[2]); oskar_log_value(log, p, 1, "Max dimensions of a grid", "(%d x %d x %d)", info->device[i].max_grid_size[0], info->device[i].max_grid_size[1], info->device[i].max_grid_size[2]); } } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/correlate/src/oskar_auto_correlate.c
<filename>oskar/correlate/src/oskar_auto_correlate.c<gh_stars>1-10 /* * Copyright (c) 2015-2018, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "correlate/oskar_auto_correlate.h" #include "correlate/oskar_auto_correlate_cuda.h" #include "correlate/oskar_auto_correlate_omp.h" #include "correlate/oskar_auto_correlate_scalar_cuda.h" #include "correlate/oskar_auto_correlate_scalar_omp.h" #include "utility/oskar_device_utils.h" #ifdef __cplusplus extern "C" { #endif void oskar_auto_correlate(oskar_Mem* vis, int n_sources, const oskar_Jones* jones, const oskar_Sky* sky, int* status) { int jones_type, base_type, location, n_stations; const oskar_Mem *J, *I, *Q, *U, *V; /* Check if safe to proceed. */ if (*status) return; /* Get the data dimensions. */ n_stations = oskar_jones_num_stations(jones); /* Check data locations. */ location = oskar_sky_mem_location(sky); if (oskar_jones_mem_location(jones) != location || oskar_mem_location(vis) != location) { *status = OSKAR_ERR_LOCATION_MISMATCH; return; } /* Check for consistent data types. */ jones_type = oskar_jones_type(jones); base_type = oskar_sky_precision(sky); if (oskar_mem_precision(vis) != base_type || oskar_type_precision(jones_type) != base_type) { *status = OSKAR_ERR_TYPE_MISMATCH; return; } if (oskar_mem_type(vis) != jones_type) { *status = OSKAR_ERR_TYPE_MISMATCH; return; } /* If neither single or double precision, return error. */ if (base_type != OSKAR_SINGLE && base_type != OSKAR_DOUBLE) { *status = OSKAR_ERR_BAD_DATA_TYPE; return; } /* Check the input dimensions. */ if (oskar_jones_num_sources(jones) < n_sources) { *status = OSKAR_ERR_DIMENSION_MISMATCH; return; } /* Get handles to arrays. */ J = oskar_jones_mem_const(jones); I = oskar_sky_I_const(sky); Q = oskar_sky_Q_const(sky); U = oskar_sky_U_const(sky); V = oskar_sky_V_const(sky); /* Select kernel. */ if (location == OSKAR_CPU) { switch (oskar_mem_type(vis)) { case OSKAR_SINGLE_COMPLEX_MATRIX: oskar_auto_correlate_omp_f(n_sources, n_stations, oskar_mem_float4c_const(J, status), oskar_mem_float_const(I, status), oskar_mem_float_const(Q, status), oskar_mem_float_const(U, status), oskar_mem_float_const(V, status), oskar_mem_float4c(vis, status)); break; case OSKAR_DOUBLE_COMPLEX_MATRIX: oskar_auto_correlate_omp_d(n_sources, n_stations, oskar_mem_double4c_const(J, status), oskar_mem_double_const(I, status), oskar_mem_double_const(Q, status), oskar_mem_double_const(U, status), oskar_mem_double_const(V, status), oskar_mem_double4c(vis, status)); break; case OSKAR_SINGLE_COMPLEX: oskar_auto_correlate_scalar_omp_f(n_sources, n_stations, oskar_mem_float2_const(J, status), oskar_mem_float_const(I, status), oskar_mem_float2(vis, status)); break; case OSKAR_DOUBLE_COMPLEX: oskar_auto_correlate_scalar_omp_d(n_sources, n_stations, oskar_mem_double2_const(J, status), oskar_mem_double_const(I, status), oskar_mem_double2(vis, status)); break; default: *status = OSKAR_ERR_BAD_DATA_TYPE; return; } } else if (location == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA switch (oskar_mem_type(vis)) { case OSKAR_SINGLE_COMPLEX_MATRIX: oskar_auto_correlate_cuda_f(n_sources, n_stations, oskar_mem_float4c_const(J, status), oskar_mem_float_const(I, status), oskar_mem_float_const(Q, status), oskar_mem_float_const(U, status), oskar_mem_float_const(V, status), oskar_mem_float4c(vis, status)); break; case OSKAR_DOUBLE_COMPLEX_MATRIX: oskar_auto_correlate_cuda_d(n_sources, n_stations, oskar_mem_double4c_const(J, status), oskar_mem_double_const(I, status), oskar_mem_double_const(Q, status), oskar_mem_double_const(U, status), oskar_mem_double_const(V, status), oskar_mem_double4c(vis, status)); break; case OSKAR_SINGLE_COMPLEX: oskar_auto_correlate_scalar_cuda_f(n_sources, n_stations, oskar_mem_float2_const(J, status), oskar_mem_float_const(I, status), oskar_mem_float2(vis, status)); break; case OSKAR_DOUBLE_COMPLEX: oskar_auto_correlate_scalar_cuda_d(n_sources, n_stations, oskar_mem_double2_const(J, status), oskar_mem_double_const(I, status), oskar_mem_double2(vis, status)); break; default: *status = OSKAR_ERR_BAD_DATA_TYPE; return; } oskar_device_check_error(status); #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } else *status = OSKAR_ERR_BAD_LOCATION; } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/convert/oskar_convert_relative_directions_to_lon_lat.h
<reponame>davepallot/OSKAR<filename>oskar/convert/oskar_convert_relative_directions_to_lon_lat.h /* * Copyright (c) 2013-2014, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_CONVERT_RELATIVE_DIRECTIONS_TO_LON_LAT_H_ #define OSKAR_CONVERT_RELATIVE_DIRECTIONS_TO_LON_LAT_H_ /** * @file oskar_convert_relative_directions_to_lon_lat.h */ #include <oskar_global.h> #ifdef __cplusplus extern "C" { #endif /** * @brief * Convert direction cosines to angles (single precision). * * @details * Returns the longitude and latitude of the supplied array of * direction cosines (l and m). * * @param[in] num_points Number of positions to evaluate. * @param[in] l Array of x-positions in cosine space. * @param[in] m Array of y-positions in cosine space. * @param[in] lon0_rad Longitude of the field centre, in radians. * @param[in] lat0_rad Latitude of the field centre, in radians. * @param[out] lon_rad Array of longitude values, in radians. * @param[out] lat_rad Array of latitude values, in radians. */ OSKAR_EXPORT void oskar_convert_relative_directions_to_lon_lat_2d_f(int num_points, const float* l, const float* m, float lon0_rad, float lat0_rad, float* lon_rad, float* lat_rad); /** * @brief * Convert direction cosines to angles (double precision). * * @details * Returns the longitude and latitude of the supplied array of * direction cosines (l and m). * * @param[in] num_points Number of positions to evaluate. * @param[in] l Array of x-positions in cosine space. * @param[in] m Array of y-positions in cosine space. * @param[in] lon0_rad Longitude of the field centre, in radians. * @param[in] lat0_rad Latitude of the field centre, in radians. * @param[out] lon_rad Array of longitude values, in radians. * @param[out] lat_rad Array of latitude values, in radians. */ OSKAR_EXPORT void oskar_convert_relative_directions_to_lon_lat_2d_d(int num_points, const double* l, const double* m, double lon0_rad, double lat0_rad, double* lon_rad, double* lat_rad); #ifdef __cplusplus } #endif #endif /* OSKAR_CONVERT_RELATIVE_DIRECTIONS_TO_LON_LAT_H_ */
davepallot/OSKAR
oskar/telescope/station/src/oskar_station_set_element_errors.c
<reponame>davepallot/OSKAR /* * Copyright (c) 2011-2016, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include <stdlib.h> #include "math/oskar_cmath.h" #include "telescope/station/private_station.h" #include "telescope/station/oskar_station.h" #ifdef __cplusplus extern "C" { #endif void oskar_station_set_element_errors(oskar_Station* dst, int index, double gain, double gain_error, double phase_offset, double phase_error, int* status) { /* Check if safe to proceed. */ if (*status) return; /* Convert phases to radians */ phase_offset *= M_PI / 180.0; phase_error *= M_PI / 180.0; /* Check range. */ if (index >= dst->num_elements) { *status = OSKAR_ERR_OUT_OF_RANGE; return; } if (oskar_station_mem_location(dst) == OSKAR_CPU) { int type; void *gain_, *gain_err_, *phase_, *phase_err_; /* Get byte pointers. */ gain_ = oskar_mem_void(dst->element_gain); gain_err_ = oskar_mem_void(dst->element_gain_error); phase_ = oskar_mem_void(dst->element_phase_offset_rad); phase_err_ = oskar_mem_void(dst->element_phase_error_rad); /* Get station model data type. */ type = oskar_station_precision(dst); if (type == OSKAR_DOUBLE) { ((double*)gain_)[index] = gain; ((double*)gain_err_)[index] = gain_error; ((double*)phase_)[index] = phase_offset; ((double*)phase_err_)[index] = phase_error; } else if (type == OSKAR_SINGLE) { ((float*)gain_)[index] = (float) gain; ((float*)gain_err_)[index] = (float) gain_error; ((float*)phase_)[index] = (float) phase_offset; ((float*)phase_err_)[index] = (float) phase_error; } else *status = OSKAR_ERR_BAD_DATA_TYPE; } else { oskar_mem_set_element_real( dst->element_gain, index, gain, status); oskar_mem_set_element_real( dst->element_gain_error, index, gain_error, status); oskar_mem_set_element_real( dst->element_phase_offset_rad, index, phase_offset, status); oskar_mem_set_element_real( dst->element_phase_error_rad, index, phase_error, status); } } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/interferometer/src/oskar_interferometer.c
/* * Copyright (c) 2011-2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "math/oskar_cmath.h" #include "convert/oskar_convert_ecef_to_station_uvw.h" #include "convert/oskar_convert_ecef_to_baseline_uvw.h" #include "convert/oskar_convert_mjd_to_gast_fast.h" #include "correlate/oskar_auto_correlate.h" #include "correlate/oskar_cross_correlate.h" #include "interferometer/oskar_evaluate_jones_R.h" #include "interferometer/oskar_evaluate_jones_Z.h" #include "interferometer/oskar_evaluate_jones_E.h" #include "interferometer/oskar_evaluate_jones_K.h" #include "interferometer/oskar_jones.h" #include "interferometer/oskar_interferometer.h" #include "log/oskar_log.h" #include "sky/oskar_sky.h" #include "telescope/oskar_telescope.h" #include "utility/oskar_cuda_mem_log.h" #include "utility/oskar_device_utils.h" #include "utility/oskar_get_memory_usage.h" #include "utility/oskar_get_num_procs.h" #include "utility/oskar_thread.h" #include "utility/oskar_timer.h" #include "vis/oskar_vis_block.h" #include "vis/oskar_vis_block_write_ms.h" #include "vis/oskar_vis_header.h" #include "vis/oskar_vis_header_write_ms.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <float.h> #ifdef _OPENMP #include <omp.h> #endif #ifdef __cplusplus extern "C" { #endif /* Memory allocated per compute device (may be either CPU or GPU). */ struct DeviceData { /* Host memory. */ oskar_VisBlock* vis_block_cpu[2]; /* On host, for copy back & write. */ /* Device memory. */ int previous_chunk_index; oskar_VisBlock* vis_block; /* Device memory block. */ oskar_Mem *u, *v, *w; oskar_Sky* chunk; /* The unmodified sky chunk being processed. */ oskar_Sky* chunk_clip; /* Copy of the chunk after horizon clipping. */ oskar_Telescope* tel; /* Telescope model, created as a copy. */ oskar_Jones *J, *R, *E, *K, *Z; oskar_StationWork* station_work; /* Timers. */ oskar_Timer* tmr_compute; /* Total time spent filling vis blocks. */ oskar_Timer* tmr_copy; /* Time spent copying data. */ oskar_Timer* tmr_clip; /* Time spent in horizon clip. */ oskar_Timer* tmr_correlate; /* Time spent correlating Jones matrices. */ oskar_Timer* tmr_join; /* Time spent combining Jones matrices. */ oskar_Timer* tmr_E; /* Time spent evaluating E-Jones. */ oskar_Timer* tmr_K; /* Time spent evaluating K-Jones. */ }; typedef struct DeviceData DeviceData; struct oskar_Interferometer { /* Settings. */ int prec, num_devices, num_gpus, *gpu_ids, num_channels, num_time_steps; int max_sources_per_chunk, max_times_per_block; int apply_horizon_clip, force_polarised_ms, zero_failed_gaussians; int coords_only; double freq_start_hz, freq_inc_hz, time_start_mjd_utc, time_inc_sec; double source_min_jy, source_max_jy; char correlation_type, *vis_name, *ms_name, *settings_path; /* State. */ int init_sky, work_unit_index, status; oskar_Mutex* mutex; oskar_Barrier* barrier; /* Sky model and telescope model. */ int num_sources_total, num_sky_chunks; oskar_Sky** sky_chunks; oskar_Telescope* tel; /* Output data and file handles. */ oskar_Log* log; oskar_VisHeader* header; oskar_MeasurementSet* ms; oskar_Binary* vis; oskar_Mem* temp; oskar_Timer* tmr_sim; /* The total time for the simulation. */ oskar_Timer* tmr_write; /* The time spent writing vis blocks. */ /* Array of DeviceData structures, one per compute device. */ DeviceData* d; }; #ifndef OSKAR_INTERFEROMETER_TYPEDEF_ #define OSKAR_INTERFEROMETER_TYPEDEF_ typedef struct oskar_Interferometer oskar_Interferometer; #endif /* Private method prototypes. */ static void sim_baselines(oskar_Interferometer* h, DeviceData* d, oskar_Sky* sky, int channel_index_block, int time_index_block, int time_index_simulation, int* status); static void free_device_data(oskar_Interferometer* h, int* status); static void set_up_device_data(oskar_Interferometer* h, int* status); static void set_up_vis_header(oskar_Interferometer* h, int* status); static void record_timing(oskar_Interferometer* h); static unsigned int disp_width(unsigned int value); static void system_mem_log(oskar_Log* log); /* Public methods. */ void oskar_interferometer_check_init(oskar_Interferometer* h, int* status) { if (*status) return; /* Check that the telescope model has been set. */ if (!h->tel) { oskar_log_error(h->log, "Telescope model not set."); *status = OSKAR_ERR_SETTINGS_TELESCOPE; return; } /* Create the visibility header if required. */ if (!h->header) set_up_vis_header(h, status); /* Calculate source parameters if required. */ if (!h->init_sky) { int i, num_failed = 0; double ra0, dec0; /* Compute source direction cosines relative to phase centre. */ ra0 = oskar_telescope_phase_centre_ra_rad(h->tel); dec0 = oskar_telescope_phase_centre_dec_rad(h->tel); for (i = 0; i < h->num_sky_chunks; ++i) { oskar_sky_evaluate_relative_directions(h->sky_chunks[i], ra0, dec0, status); /* Evaluate extended source parameters. */ oskar_sky_evaluate_gaussian_source_parameters(h->sky_chunks[i], h->zero_failed_gaussians, ra0, dec0, &num_failed, status); } if (num_failed > 0) { if (h->zero_failed_gaussians) oskar_log_warning(h->log, "Gaussian ellipse solution failed " "for %i sources. These will have their fluxes " "set to zero.", num_failed); else oskar_log_warning(h->log, "Gaussian ellipse solution failed " "for %i sources. These will be simulated " "as point sources.", num_failed); } h->init_sky = 1; } /* Check that each compute device has been set up. */ set_up_device_data(h, status); } int oskar_interferometer_coords_only(const oskar_Interferometer* h) { return h->coords_only; } oskar_Interferometer* oskar_interferometer_create(int precision, int* status) { oskar_Interferometer* h = 0; h = (oskar_Interferometer*) calloc(1, sizeof(oskar_Interferometer)); h->prec = precision; h->tmr_sim = oskar_timer_create(OSKAR_TIMER_NATIVE); h->tmr_write = oskar_timer_create(OSKAR_TIMER_NATIVE); h->temp = oskar_mem_create(precision, OSKAR_CPU, 0, status); h->mutex = oskar_mutex_create(); h->barrier = oskar_barrier_create(0); /* Set sensible defaults. */ h->max_sources_per_chunk = 16384; oskar_interferometer_set_gpus(h, -1, 0, status); oskar_interferometer_set_num_devices(h, -1); oskar_interferometer_set_correlation_type(h, "Cross-correlations", status); oskar_interferometer_set_horizon_clip(h, 1); oskar_interferometer_set_source_flux_range(h, -DBL_MAX, DBL_MAX); oskar_interferometer_set_max_times_per_block(h, 10); return h; } oskar_VisBlock* oskar_interferometer_finalise_block(oskar_Interferometer* h, int block_index, int* status) { int i, i_active; oskar_VisBlock *b0 = 0, *b = 0; if (*status) return 0; /* The visibilities must be copied back * at the end of the block simulation. */ /* Combine all vis blocks into the first one. */ i_active = (block_index + 1) % 2; b0 = h->d[0].vis_block_cpu[!i_active]; if (!h->coords_only) { oskar_Mem *xc0 = 0, *ac0 = 0; xc0 = oskar_vis_block_cross_correlations(b0); ac0 = oskar_vis_block_auto_correlations(b0); for (i = 1; i < h->num_devices; ++i) { b = h->d[i].vis_block_cpu[!i_active]; if (oskar_vis_block_has_cross_correlations(b)) oskar_mem_add(xc0, xc0, oskar_vis_block_cross_correlations(b), oskar_mem_length(xc0), status); if (oskar_vis_block_has_auto_correlations(b)) oskar_mem_add(ac0, ac0, oskar_vis_block_auto_correlations(b), oskar_mem_length(ac0), status); } } /* Calculate baseline uvw coordinates for the block. */ if (oskar_vis_block_has_cross_correlations(b0)) { const oskar_Mem *x, *y, *z; x = oskar_telescope_station_measured_x_offset_ecef_metres_const(h->tel); y = oskar_telescope_station_measured_y_offset_ecef_metres_const(h->tel); z = oskar_telescope_station_measured_z_offset_ecef_metres_const(h->tel); oskar_convert_ecef_to_baseline_uvw( oskar_telescope_num_stations(h->tel), x, y, z, oskar_telescope_phase_centre_ra_rad(h->tel), oskar_telescope_phase_centre_dec_rad(h->tel), oskar_vis_block_num_times(b0), oskar_vis_header_time_start_mjd_utc(h->header), oskar_vis_header_time_inc_sec(h->header) / 86400.0, oskar_vis_block_start_time_index(b0), oskar_vis_block_baseline_uu_metres(b0), oskar_vis_block_baseline_vv_metres(b0), oskar_vis_block_baseline_ww_metres(b0), h->temp, status); } /* Add uncorrelated system noise to the combined visibilities. */ if (!h->coords_only) { oskar_vis_block_add_system_noise(b0, h->header, h->tel, block_index, h->temp, status); } /* Return a pointer to the block. */ return b0; } void oskar_interferometer_finalise(oskar_Interferometer* h, int* status) { oskar_interferometer_reset_cache(h, status); } void oskar_interferometer_free(oskar_Interferometer* h, int* status) { int i; if (!h) return; oskar_interferometer_reset_cache(h, status); for (i = 0; i < h->num_gpus; ++i) { oskar_device_set(h->gpu_ids[i], status); oskar_device_reset(); } for (i = 0; i < h->num_sky_chunks; ++i) oskar_sky_free(h->sky_chunks[i], status); oskar_telescope_free(h->tel, status); oskar_mem_free(h->temp, status); oskar_timer_free(h->tmr_sim); oskar_timer_free(h->tmr_write); oskar_mutex_free(h->mutex); oskar_barrier_free(h->barrier); free(h->sky_chunks); free(h->gpu_ids); free(h->vis_name); free(h->ms_name); free(h->settings_path); free(h->d); free(h); } int oskar_interferometer_num_devices(const oskar_Interferometer* h) { return h ? h->num_devices : 0; } int oskar_interferometer_num_gpus(const oskar_Interferometer* h) { return h ? h->num_gpus : 0; } int oskar_interferometer_num_vis_blocks(const oskar_Interferometer* h) { return (h->num_time_steps + h->max_times_per_block - 1) / h->max_times_per_block; } void oskar_interferometer_reset_cache(oskar_Interferometer* h, int* status) { free_device_data(h, status); oskar_binary_free(h->vis); oskar_vis_header_free(h->header, status); #ifndef OSKAR_NO_MS oskar_ms_close(h->ms); #endif h->vis = 0; h->header = 0; h->ms = 0; } void oskar_interferometer_reset_work_unit_index(oskar_Interferometer* h) { h->work_unit_index = 0; } void oskar_interferometer_run_block(oskar_Interferometer* h, int block_index, int device_id, int* status) { double obs_start_mjd, dt_dump_days; int i_active, time_index_start, time_index_end; int num_channels, num_times_block, total_chunks, total_times; DeviceData* d; if (*status) return; /* Check that initialisation has happened. We can't initialise here, * as we're already multi-threaded at this point. */ if (!h->header) { *status = OSKAR_ERR_MEMORY_NOT_ALLOCATED; oskar_log_error(h->log, "Simulator not initalised. " "Call oskar_interferometer_check_init() first."); return; } /* Set the GPU to use. (Supposed to be a very low-overhead call.) */ if (device_id >= 0 && device_id < h->num_gpus) oskar_device_set(h->gpu_ids[device_id], status); /* Clear the visibility block. */ i_active = block_index % 2; /* Index of the active buffer. */ d = &(h->d[device_id]); oskar_timer_resume(d->tmr_compute); oskar_vis_block_clear(d->vis_block, status); /* Set the visibility block meta-data. */ total_chunks = h->num_sky_chunks; num_channels = h->num_channels; total_times = h->num_time_steps; obs_start_mjd = h->time_start_mjd_utc; dt_dump_days = h->time_inc_sec / 86400.0; time_index_start = block_index * h->max_times_per_block; time_index_end = time_index_start + h->max_times_per_block - 1; if (time_index_end >= total_times) time_index_end = total_times - 1; num_times_block = 1 + time_index_end - time_index_start; /* Set the number of active times in the block. */ oskar_vis_block_set_num_times(d->vis_block, num_times_block, status); oskar_vis_block_set_start_time_index(d->vis_block, time_index_start); /* Go though all possible work units in the block. A work unit is defined * as the simulation for one time and one sky chunk. */ while (!h->coords_only) { oskar_Sky* sky; int i_work_unit, i_chunk, i_time, i_channel, sim_time_idx; oskar_mutex_lock(h->mutex); i_work_unit = (h->work_unit_index)++; oskar_mutex_unlock(h->mutex); if ((i_work_unit >= num_times_block * total_chunks) || *status) break; /* Convert slice index to chunk/time index. */ i_chunk = i_work_unit / num_times_block; i_time = i_work_unit - i_chunk * num_times_block; sim_time_idx = time_index_start + i_time; /* Copy sky chunk to device only if different from the previous one. */ if (i_chunk != d->previous_chunk_index) { oskar_timer_resume(d->tmr_copy); oskar_sky_copy(d->chunk, h->sky_chunks[i_chunk], status); oskar_timer_pause(d->tmr_copy); } sky = h->apply_horizon_clip ? d->chunk_clip : d->chunk; /* Apply horizon clip if required. */ if (h->apply_horizon_clip) { double gast, mjd; mjd = obs_start_mjd + dt_dump_days * (sim_time_idx + 0.5); gast = oskar_convert_mjd_to_gast_fast(mjd); oskar_timer_resume(d->tmr_clip); oskar_sky_horizon_clip(d->chunk_clip, d->chunk, d->tel, gast, d->station_work, status); oskar_timer_pause(d->tmr_clip); } /* Simulate all baselines for all channels for this time and chunk. */ for (i_channel = 0; i_channel < num_channels; ++i_channel) { if (*status) break; if (h->log) { oskar_mutex_lock(h->mutex); oskar_log_message(h->log, 'S', 1, "Time %*i/%i, " "Chunk %*i/%i, Channel %*i/%i [Device %i, %i sources]", disp_width(total_times), sim_time_idx + 1, total_times, disp_width(total_chunks), i_chunk + 1, total_chunks, disp_width(num_channels), i_channel + 1, num_channels, device_id, oskar_sky_num_sources(sky)); oskar_mutex_unlock(h->mutex); } sim_baselines(h, d, sky, i_channel, i_time, sim_time_idx, status); } d->previous_chunk_index = i_chunk; } /* Copy the visibility block to host memory. */ oskar_timer_resume(d->tmr_copy); oskar_vis_block_copy(d->vis_block_cpu[i_active], d->vis_block, status); oskar_timer_pause(d->tmr_copy); oskar_timer_pause(d->tmr_compute); } struct ThreadArgs { oskar_Interferometer* h; int num_threads, thread_id; }; typedef struct ThreadArgs ThreadArgs; static void* run_blocks(void* arg) { oskar_Interferometer* h; int b, thread_id, device_id, num_blocks, num_threads, *status; /* Get thread function arguments. */ h = ((ThreadArgs*)arg)->h; num_threads = ((ThreadArgs*)arg)->num_threads; thread_id = ((ThreadArgs*)arg)->thread_id; device_id = thread_id - 1; status = &(h->status); #ifdef _OPENMP /* Disable any nested parallelism. */ omp_set_nested(0); omp_set_num_threads(1); #endif /* Loop over blocks of observation time, running simulation and file * writing one block at a time. Simulation and file output are overlapped * by using double buffering, and a dedicated thread is used for file * output. * * Thread 0 is used for file writes. * Threads 1 to n (mapped to compute devices) do the simulation. * * Note that no write is launched on the first loop counter (as no * data are ready yet) and no simulation is performed for the last loop * counter (which corresponds to the last block + 1) as this iteration * simply writes the last block. */ num_blocks = oskar_interferometer_num_vis_blocks(h); for (b = 0; b < num_blocks + 1; ++b) { if ((thread_id > 0 || num_threads == 1) && b < num_blocks) oskar_interferometer_run_block(h, b, device_id, status); if (thread_id == 0 && b > 0) { oskar_VisBlock* block; block = oskar_interferometer_finalise_block(h, b - 1, status); oskar_interferometer_write_block(h, block, b - 1, status); } /* Barrier 1: Reset work unit index and print status. */ oskar_barrier_wait(h->barrier); if (thread_id == 0) { oskar_interferometer_reset_work_unit_index(h); if (b < num_blocks && h->log && !*status) oskar_log_message(h->log, 'S', 0, "Block %*i/%i (%3.0f%%) " "complete. Simulation time elapsed: %.3f s", disp_width(num_blocks), b+1, num_blocks, 100.0 * (b+1) / (double)num_blocks, oskar_timer_elapsed(h->tmr_sim)); } /* Barrier 2: Synchronise before moving to the next block. */ oskar_barrier_wait(h->barrier); } return 0; } void oskar_interferometer_run(oskar_Interferometer* h, int* status) { int i, num_threads; oskar_Thread** threads = 0; ThreadArgs* args = 0; if (*status || !h) return; /* Check the visibilities are going somewhere. */ if (!h->vis_name #ifndef OSKAR_NO_MS && !h->ms_name #endif ) { oskar_log_error(h->log, "No output file specified."); #ifdef OSKAR_NO_MS if (h->ms_name) oskar_log_error(h->log, "OSKAR was compiled without Measurement Set support."); #endif *status = OSKAR_ERR_FILE_IO; return; } /* Initialise if required. */ oskar_interferometer_check_init(h, status); /* Set up worker threads. */ num_threads = h->num_devices + 1; oskar_barrier_set_num_threads(h->barrier, num_threads); threads = (oskar_Thread**) calloc(num_threads, sizeof(oskar_Thread*)); args = (ThreadArgs*) calloc(num_threads, sizeof(ThreadArgs)); for (i = 0; i < num_threads; ++i) { args[i].h = h; args[i].num_threads = num_threads; args[i].thread_id = i; } /* Record memory usage. */ if (h->log && !*status) { oskar_log_section(h->log, 'M', "Initial memory usage"); #ifdef OSKAR_HAVE_CUDA for (i = 0; i < h->num_gpus; ++i) oskar_cuda_mem_log(h->log, 0, h->gpu_ids[i]); #endif system_mem_log(h->log); oskar_log_section(h->log, 'M', "Starting simulation..."); } /* Start simulation timer. */ oskar_timer_start(h->tmr_sim); /* Set status code. */ h->status = *status; /* Start the worker threads. */ oskar_interferometer_reset_work_unit_index(h); for (i = 0; i < num_threads; ++i) threads[i] = oskar_thread_create(run_blocks, (void*)&args[i], 0); /* Wait for worker threads to finish. */ for (i = 0; i < num_threads; ++i) { oskar_thread_join(threads[i]); oskar_thread_free(threads[i]); } free(threads); free(args); /* Get status code. */ *status = h->status; /* Record memory usage. */ if (h->log && !*status) { oskar_log_section(h->log, 'M', "Final memory usage"); #ifdef OSKAR_HAVE_CUDA for (i = 0; i < h->num_gpus; ++i) oskar_cuda_mem_log(h->log, 0, h->gpu_ids[i]); #endif system_mem_log(h->log); } /* If there are sources in the simulation and the station beam is not * normalised to 1.0 at the phase centre, the values of noise RMS * may give a very unexpected S/N ratio! * The alternative would be to scale the noise to match the station * beam gain but that would require knowledge of the station beam * amplitude at the phase centre for each time and channel. */ if (h->log && oskar_telescope_noise_enabled(h->tel) && !*status) { int have_sources, amp_calibrated; have_sources = (h->num_sky_chunks > 0 && oskar_sky_num_sources(h->sky_chunks[0]) > 0); amp_calibrated = oskar_station_normalise_final_beam( oskar_telescope_station_const(h->tel, 0)); if (have_sources && !amp_calibrated) { const char* a = "WARNING: System noise added to visibilities"; const char* b = "without station beam normalisation enabled."; const char* c = "This will give an invalid signal to noise ratio."; oskar_log_line(h->log, 'W', ' '); oskar_log_line(h->log, 'W', '*'); oskar_log_message(h->log, 'W', -1, a); oskar_log_message(h->log, 'W', -1, b); oskar_log_message(h->log, 'W', -1, c); oskar_log_line(h->log, 'W', '*'); oskar_log_line(h->log, 'W', ' '); } } /* Record times and summarise output files. */ if (h->log && !*status) { size_t log_size = 0; char* log_data; oskar_log_set_value_width(h->log, 25); record_timing(h); oskar_log_section(h->log, 'M', "Simulation complete"); oskar_log_message(h->log, 'M', 0, "Output(s):"); if (h->vis_name) oskar_log_value(h->log, 'M', 1, "OSKAR binary file", "%s", h->vis_name); if (h->ms_name) oskar_log_value(h->log, 'M', 1, "Measurement Set", "%s", h->ms_name); /* Write simulation log to the output files. */ log_data = oskar_log_file_data(h->log, &log_size); #ifndef OSKAR_NO_MS if (h->ms) oskar_ms_add_history(h->ms, "OSKAR_LOG", log_data, log_size); #endif if (h->vis) oskar_binary_write(h->vis, OSKAR_CHAR, OSKAR_TAG_GROUP_RUN, OSKAR_TAG_RUN_LOG, 0, log_size, log_data, status); free(log_data); } /* Finalise. */ oskar_interferometer_finalise(h, status); } void oskar_interferometer_set_coords_only(oskar_Interferometer* h, int value, int* status) { h->coords_only = value; oskar_interferometer_check_init(h, status); } void oskar_interferometer_set_correlation_type(oskar_Interferometer* h, const char* type, int* status) { if (*status) return; if (!strncmp(type, "A", 1) || !strncmp(type, "a", 1)) h->correlation_type = 'A'; else if (!strncmp(type, "B", 1) || !strncmp(type, "b", 1)) h->correlation_type = 'B'; else if (!strncmp(type, "C", 1) || !strncmp(type, "c", 1)) h->correlation_type = 'C'; else *status = OSKAR_ERR_INVALID_ARGUMENT; } void oskar_interferometer_set_force_polarised_ms(oskar_Interferometer* h, int value) { h->force_polarised_ms = value; } void oskar_interferometer_set_gpus(oskar_Interferometer* h, int num, const int* ids, int* status) { int i, num_gpus_avail; if (*status || !h) return; free_device_data(h, status); num_gpus_avail = oskar_device_count(status); if (*status) return; if (num < 0) { h->num_gpus = num_gpus_avail; h->gpu_ids = (int*) realloc(h->gpu_ids, h->num_gpus * sizeof(int)); for (i = 0; i < h->num_gpus; ++i) h->gpu_ids[i] = i; } else if (num > 0) { if (num > num_gpus_avail) { oskar_log_error(h->log, "More GPUs were requested than found."); *status = OSKAR_ERR_COMPUTE_DEVICES; return; } h->num_gpus = num; h->gpu_ids = (int*) realloc(h->gpu_ids, h->num_gpus * sizeof(int)); for (i = 0; i < h->num_gpus; ++i) h->gpu_ids[i] = ids[i]; } else /* num == 0 */ { free(h->gpu_ids); h->gpu_ids = 0; h->num_gpus = 0; } for (i = 0; i < h->num_gpus; ++i) { oskar_device_set(h->gpu_ids[i], status); if (*status) return; } } void oskar_interferometer_set_horizon_clip(oskar_Interferometer* h, int value) { h->apply_horizon_clip = value; } void oskar_interferometer_set_log(oskar_Interferometer* h, oskar_Log* log) { h->log = log; } void oskar_interferometer_set_max_sources_per_chunk(oskar_Interferometer* h, int value) { h->max_sources_per_chunk = value; } void oskar_interferometer_set_max_times_per_block(oskar_Interferometer* h, int value) { h->max_times_per_block = value; } void oskar_interferometer_set_num_devices(oskar_Interferometer* h, int value) { int status = 0; free_device_data(h, &status); if (value < 1) value = (h->num_gpus == 0) ? (oskar_get_num_procs() - 1) : h->num_gpus; if (value < 1) value = 1; h->num_devices = value; h->d = (DeviceData*) realloc(h->d, h->num_devices * sizeof(DeviceData)); memset(h->d, 0, h->num_devices * sizeof(DeviceData)); } void oskar_interferometer_set_observation_frequency(oskar_Interferometer* h, double start_hz, double inc_hz, int num_channels) { h->freq_start_hz = start_hz; h->freq_inc_hz = inc_hz; h->num_channels = num_channels; } void oskar_interferometer_set_observation_time(oskar_Interferometer* h, double time_start_mjd_utc, double inc_sec, int num_time_steps) { h->time_start_mjd_utc = time_start_mjd_utc; h->time_inc_sec = inc_sec; h->num_time_steps = num_time_steps; } void oskar_interferometer_set_settings_path(oskar_Interferometer* h, const char* filename) { int len; len = (int) strlen(filename); if (len == 0) return; free(h->settings_path); h->settings_path = calloc(1 + len, 1); strcpy(h->settings_path, filename); } void oskar_interferometer_set_sky_model(oskar_Interferometer* h, const oskar_Sky* sky, int* status) { int i; if (*status || !h || !sky) return; /* Clear the old chunk set. */ for (i = 0; i < h->num_sky_chunks; ++i) oskar_sky_free(h->sky_chunks[i], status); free(h->sky_chunks); h->sky_chunks = 0; h->num_sky_chunks = 0; /* Split up the sky model into chunks and store them. */ h->num_sources_total = oskar_sky_num_sources(sky); if (h->num_sources_total > 0) oskar_sky_append_to_set(&h->num_sky_chunks, &h->sky_chunks, h->max_sources_per_chunk, sky, status); h->init_sky = 0; /* Print summary data. */ if (h->log) { oskar_log_section(h->log, 'M', "Sky model summary"); oskar_log_value(h->log, 'M', 0, "Num. sources", "%d", h->num_sources_total); oskar_log_value(h->log, 'M', 0, "Num. chunks", "%d", h->num_sky_chunks); } } void oskar_interferometer_set_telescope_model(oskar_Interferometer* h, const oskar_Telescope* model, int* status) { if (*status || !h || !model) return; /* Check the model is not empty. */ if (oskar_telescope_num_stations(model) == 0) { oskar_log_error(h->log, "Telescope model is empty."); *status = OSKAR_ERR_SETTINGS_TELESCOPE; return; } /* Remove any existing telescope model, and copy the new one. */ oskar_telescope_free(h->tel, status); h->tel = oskar_telescope_create_copy(model, OSKAR_CPU, status); /* Analyse the telescope model. */ oskar_telescope_analyse(h->tel, status); if (h->log) oskar_telescope_log_summary(h->tel, h->log, status); } void oskar_interferometer_set_output_vis_file(oskar_Interferometer* h, const char* filename) { int len; len = (int) strlen(filename); free(h->vis_name); h->vis_name = 0; if (len == 0) return; h->vis_name = calloc(1 + len, 1); strcpy(h->vis_name, filename); } void oskar_interferometer_set_output_measurement_set(oskar_Interferometer* h, const char* filename) { int len; len = (int) strlen(filename); free(h->ms_name); h->ms_name = 0; if (len == 0) return; h->ms_name = calloc(6 + len, 1); if ((len >= 3) && ( !strcmp(&(filename[len-3]), ".MS") || !strcmp(&(filename[len-3]), ".ms") )) strcpy(h->ms_name, filename); else sprintf(h->ms_name, "%s.MS", filename); } void oskar_interferometer_set_source_flux_range(oskar_Interferometer* h, double min_jy, double max_jy) { h->source_min_jy = min_jy; h->source_max_jy = max_jy; } void oskar_interferometer_set_zero_failed_gaussians(oskar_Interferometer* h, int value) { h->zero_failed_gaussians = value; } const oskar_VisHeader* oskar_interferometer_vis_header(oskar_Interferometer* h) { return h->header; } void oskar_interferometer_write_block(oskar_Interferometer* h, const oskar_VisBlock* block, int block_index, int* status) { if (*status) return; /* Open files only if required, and write the block into them. */ oskar_timer_resume(h->tmr_write); #ifndef OSKAR_NO_MS if (h->ms_name && !h->ms) h->ms = oskar_vis_header_write_ms(h->header, h->ms_name, OSKAR_TRUE, h->force_polarised_ms, status); if (h->ms) oskar_vis_block_write_ms(block, h->header, h->ms, status); #endif if (h->vis_name && !h->vis) h->vis = oskar_vis_header_write(h->header, h->vis_name, status); if (h->vis) oskar_vis_block_write(block, h->vis, block_index, status); oskar_timer_pause(h->tmr_write); } /* Private methods. */ static void sim_baselines(oskar_Interferometer* h, DeviceData* d, oskar_Sky* sky, int channel_index_block, int time_index_block, int time_index_simulation, int* status) { int num_baselines, num_stations, num_src, num_times_block, num_channels; double dt_dump_days, t_start, t_dump, gast, frequency, ra0, dec0; const oskar_Mem *x, *y, *z; oskar_Mem* alias = 0; /* Get dimensions. */ num_baselines = oskar_telescope_num_baselines(d->tel); num_stations = oskar_telescope_num_stations(d->tel); num_src = oskar_sky_num_sources(sky); num_times_block = oskar_vis_block_num_times(d->vis_block); num_channels = oskar_vis_block_num_channels(d->vis_block); /* Return if there are no sources in the chunk, * or if block time index requested is outside the valid range. */ if (num_src == 0 || time_index_block >= num_times_block) return; /* Get the time and frequency of the visibility slice being simulated. */ dt_dump_days = h->time_inc_sec / 86400.0; t_start = h->time_start_mjd_utc; t_dump = t_start + dt_dump_days * (time_index_simulation + 0.5); gast = oskar_convert_mjd_to_gast_fast(t_dump); frequency = h->freq_start_hz + channel_index_block * h->freq_inc_hz; /* Scale source fluxes with spectral index and rotation measure. */ oskar_sky_scale_flux_with_frequency(sky, frequency, status); /* Evaluate station u,v,w coordinates. */ ra0 = oskar_telescope_phase_centre_ra_rad(d->tel); dec0 = oskar_telescope_phase_centre_dec_rad(d->tel); x = oskar_telescope_station_true_x_offset_ecef_metres_const(d->tel); y = oskar_telescope_station_true_y_offset_ecef_metres_const(d->tel); z = oskar_telescope_station_true_z_offset_ecef_metres_const(d->tel); oskar_convert_ecef_to_station_uvw(num_stations, x, y, z, ra0, dec0, gast, d->u, d->v, d->w, status); /* Set dimensions of Jones matrices. */ if (d->R) oskar_jones_set_size(d->R, num_stations, num_src, status); if (d->Z) oskar_jones_set_size(d->Z, num_stations, num_src, status); oskar_jones_set_size(d->J, num_stations, num_src, status); oskar_jones_set_size(d->E, num_stations, num_src, status); oskar_jones_set_size(d->K, num_stations, num_src, status); /* Evaluate station beam (Jones E: may be matrix). */ oskar_timer_resume(d->tmr_E); oskar_evaluate_jones_E(d->E, num_src, OSKAR_RELATIVE_DIRECTIONS, oskar_sky_l(sky), oskar_sky_m(sky), oskar_sky_n(sky), d->tel, gast, frequency, d->station_work, time_index_simulation, status); oskar_timer_pause(d->tmr_E); #if 0 /* Evaluate ionospheric phase (Jones Z: scalar) and join with Jones E. * NOTE this is currently only a CPU implementation. */ if (d->Z) { oskar_evaluate_jones_Z(d->Z, num_src, sky, d->tel, &settings->ionosphere, gast, frequency, &(d->workJonesZ), status); oskar_timer_resume(d->tmr_join); oskar_jones_join(d->E, d->Z, d->E, status); oskar_timer_pause(d->tmr_join); } #endif /* Evaluate parallactic angle (Jones R: matrix), and join with Jones Z*E. * TODO Move this into station beam evaluation instead. */ if (d->R) { oskar_timer_resume(d->tmr_E); oskar_evaluate_jones_R(d->R, num_src, oskar_sky_ra_rad_const(sky), oskar_sky_dec_rad_const(sky), d->tel, gast, status); oskar_timer_pause(d->tmr_E); oskar_timer_resume(d->tmr_join); oskar_jones_join(d->R, d->E, d->R, status); oskar_timer_pause(d->tmr_join); } /* Evaluate interferometer phase (Jones K: scalar). */ oskar_timer_resume(d->tmr_K); oskar_evaluate_jones_K(d->K, num_src, oskar_sky_l_const(sky), oskar_sky_m_const(sky), oskar_sky_n_const(sky), d->u, d->v, d->w, frequency, oskar_sky_I_const(sky), h->source_min_jy, h->source_max_jy, status); oskar_timer_pause(d->tmr_K); /* Join Jones K with <NAME>. */ oskar_timer_resume(d->tmr_join); oskar_jones_join(d->J, d->K, d->R ? d->R : d->E, status); oskar_timer_pause(d->tmr_join); /* Create alias for auto/cross-correlations. */ oskar_timer_resume(d->tmr_correlate); alias = oskar_mem_create_alias(0, 0, 0, status); /* Auto-correlate for this time and channel. */ if (oskar_vis_block_has_auto_correlations(d->vis_block)) { oskar_mem_set_alias(alias, oskar_vis_block_auto_correlations(d->vis_block), num_stations * (num_channels * time_index_block + channel_index_block), num_stations, status); oskar_auto_correlate(alias, num_src, d->J, sky, status); } /* Cross-correlate for this time and channel. */ if (oskar_vis_block_has_cross_correlations(d->vis_block)) { oskar_mem_set_alias(alias, oskar_vis_block_cross_correlations(d->vis_block), num_baselines * (num_channels * time_index_block + channel_index_block), num_baselines, status); oskar_cross_correlate(alias, num_src, d->J, sky, d->tel, d->u, d->v, d->w, gast, frequency, status); } /* Free alias for auto/cross-correlations. */ oskar_mem_free(alias, status); oskar_timer_pause(d->tmr_correlate); } static void set_up_vis_header(oskar_Interferometer* h, int* status) { int num_stations, vis_type; const double rad2deg = 180.0/M_PI; int write_autocorr = 0, write_crosscorr = 0; if (*status) return; /* Check type of correlations to produce. */ if (h->correlation_type == 'C') write_crosscorr = 1; else if (h->correlation_type == 'A') write_autocorr = 1; else if (h->correlation_type == 'B') { write_autocorr = 1; write_crosscorr = 1; } /* Create visibility header. */ num_stations = oskar_telescope_num_stations(h->tel); vis_type = h->prec | OSKAR_COMPLEX; if (oskar_telescope_pol_mode(h->tel) == OSKAR_POL_MODE_FULL) vis_type |= OSKAR_MATRIX; h->header = oskar_vis_header_create(vis_type, h->prec, h->max_times_per_block, h->num_time_steps, h->num_channels, h->num_channels, num_stations, write_autocorr, write_crosscorr, status); /* Add metadata from settings. */ oskar_vis_header_set_freq_start_hz(h->header, h->freq_start_hz); oskar_vis_header_set_freq_inc_hz(h->header, h->freq_inc_hz); oskar_vis_header_set_time_start_mjd_utc(h->header, h->time_start_mjd_utc); oskar_vis_header_set_time_inc_sec(h->header, h->time_inc_sec); /* Add settings file contents if defined. */ if (h->settings_path) { oskar_Mem* temp; temp = oskar_mem_read_binary_raw(h->settings_path, OSKAR_CHAR, OSKAR_CPU, status); oskar_mem_copy(oskar_vis_header_settings(h->header), temp, status); oskar_mem_free(temp, status); } /* Copy other metadata from telescope model. */ oskar_vis_header_set_time_average_sec(h->header, oskar_telescope_time_average_sec(h->tel)); oskar_vis_header_set_channel_bandwidth_hz(h->header, oskar_telescope_channel_bandwidth_hz(h->tel)); oskar_vis_header_set_phase_centre(h->header, 0, oskar_telescope_phase_centre_ra_rad(h->tel) * rad2deg, oskar_telescope_phase_centre_dec_rad(h->tel) * rad2deg); oskar_vis_header_set_telescope_centre(h->header, oskar_telescope_lon_rad(h->tel) * rad2deg, oskar_telescope_lat_rad(h->tel) * rad2deg, oskar_telescope_alt_metres(h->tel)); oskar_mem_copy(oskar_vis_header_station_x_offset_ecef_metres(h->header), oskar_telescope_station_true_x_offset_ecef_metres_const(h->tel), status); oskar_mem_copy(oskar_vis_header_station_y_offset_ecef_metres(h->header), oskar_telescope_station_true_y_offset_ecef_metres_const(h->tel), status); oskar_mem_copy(oskar_vis_header_station_z_offset_ecef_metres(h->header), oskar_telescope_station_true_z_offset_ecef_metres_const(h->tel), status); } static void set_up_device_data(oskar_Interferometer* h, int* status) { int i, dev_loc, complx, vistype, num_stations, num_src; if (*status) return; /* Get local variables. */ num_stations = oskar_telescope_num_stations(h->tel); num_src = h->max_sources_per_chunk; complx = (h->prec) | OSKAR_COMPLEX; vistype = complx; if (oskar_telescope_pol_mode(h->tel) == OSKAR_POL_MODE_FULL) vistype |= OSKAR_MATRIX; /* Expand the number of devices to the number of selected GPUs, * if required. */ if (h->num_devices < h->num_gpus) oskar_interferometer_set_num_devices(h, h->num_gpus); for (i = 0; i < h->num_devices; ++i) { DeviceData* d = &h->d[i]; d->previous_chunk_index = -1; /* Select the device. */ if (i < h->num_gpus) { oskar_device_set(h->gpu_ids[i], status); dev_loc = OSKAR_GPU; } else { dev_loc = OSKAR_CPU; } /* Timers. */ if (!d->tmr_compute) { const int timer_type = (dev_loc == OSKAR_GPU) ? OSKAR_TIMER_CUDA : OSKAR_TIMER_NATIVE; d->tmr_compute = oskar_timer_create(timer_type); d->tmr_copy = oskar_timer_create(timer_type); d->tmr_clip = oskar_timer_create(timer_type); d->tmr_E = oskar_timer_create(timer_type); d->tmr_K = oskar_timer_create(timer_type); d->tmr_join = oskar_timer_create(timer_type); d->tmr_correlate = oskar_timer_create(timer_type); } /* Visibility blocks. */ if (!d->vis_block) { d->vis_block = oskar_vis_block_create_from_header(dev_loc, h->header, status); d->vis_block_cpu[0] = oskar_vis_block_create_from_header(OSKAR_CPU, h->header, status); d->vis_block_cpu[1] = oskar_vis_block_create_from_header(OSKAR_CPU, h->header, status); } oskar_vis_block_clear(d->vis_block, status); oskar_vis_block_clear(d->vis_block_cpu[0], status); oskar_vis_block_clear(d->vis_block_cpu[1], status); /* Device scratch memory. */ if (!d->tel) { d->u = oskar_mem_create(h->prec, dev_loc, num_stations, status); d->v = oskar_mem_create(h->prec, dev_loc, num_stations, status); d->w = oskar_mem_create(h->prec, dev_loc, num_stations, status); d->chunk = oskar_sky_create(h->prec, dev_loc, num_src, status); d->chunk_clip = oskar_sky_create(h->prec, dev_loc, num_src, status); d->tel = oskar_telescope_create_copy(h->tel, dev_loc, status); d->J = oskar_jones_create(vistype, dev_loc, num_stations, num_src, status); d->R = oskar_type_is_matrix(vistype) ? oskar_jones_create(vistype, dev_loc, num_stations, num_src, status) : 0; d->E = oskar_jones_create(vistype, dev_loc, num_stations, num_src, status); d->K = oskar_jones_create(complx, dev_loc, num_stations, num_src, status); d->Z = 0; d->station_work = oskar_station_work_create(h->prec, dev_loc, status); } } } static void free_device_data(oskar_Interferometer* h, int* status) { int i; if (!h->d) return; for (i = 0; i < h->num_devices; ++i) { DeviceData* d = &(h->d[i]); if (!d) continue; if (i < h->num_gpus) oskar_device_set(h->gpu_ids[i], status); oskar_timer_free(d->tmr_compute); oskar_timer_free(d->tmr_copy); oskar_timer_free(d->tmr_clip); oskar_timer_free(d->tmr_E); oskar_timer_free(d->tmr_K); oskar_timer_free(d->tmr_join); oskar_timer_free(d->tmr_correlate); oskar_vis_block_free(d->vis_block_cpu[0], status); oskar_vis_block_free(d->vis_block_cpu[1], status); oskar_vis_block_free(d->vis_block, status); oskar_mem_free(d->u, status); oskar_mem_free(d->v, status); oskar_mem_free(d->w, status); oskar_sky_free(d->chunk, status); oskar_sky_free(d->chunk_clip, status); oskar_telescope_free(d->tel, status); oskar_station_work_free(d->station_work, status); oskar_jones_free(d->J, status); oskar_jones_free(d->E, status); oskar_jones_free(d->K, status); oskar_jones_free(d->R, status); memset(d, 0, sizeof(DeviceData)); } } static void record_timing(oskar_Interferometer* h) { /* Obtain component times. */ int i; double t_copy = 0., t_clip = 0., t_E = 0., t_K = 0., t_join = 0.; double t_correlate = 0., t_compute = 0., t_components = 0.; double *compute_times; compute_times = (double*) calloc(h->num_devices, sizeof(double)); for (i = 0; i < h->num_devices; ++i) { compute_times[i] = oskar_timer_elapsed(h->d[i].tmr_compute); t_copy += oskar_timer_elapsed(h->d[i].tmr_copy); t_clip += oskar_timer_elapsed(h->d[i].tmr_clip); t_join += oskar_timer_elapsed(h->d[i].tmr_join); t_E += oskar_timer_elapsed(h->d[i].tmr_E); t_K += oskar_timer_elapsed(h->d[i].tmr_K); t_correlate += oskar_timer_elapsed(h->d[i].tmr_correlate); t_compute += compute_times[i]; } t_components = t_copy + t_clip + t_E + t_K + t_join + t_correlate; /* Record time taken. */ oskar_log_section(h->log, 'M', "Simulation timing"); oskar_log_value(h->log, 'M', 0, "Total wall time", "%.3f s", oskar_timer_elapsed(h->tmr_sim)); for (i = 0; i < h->num_devices; ++i) oskar_log_value(h->log, 'M', 0, "Compute", "%.3f s [Device %i]", compute_times[i], i); oskar_log_value(h->log, 'M', 0, "Write", "%.3f s", oskar_timer_elapsed(h->tmr_write)); oskar_log_message(h->log, 'M', 0, "Compute components:"); oskar_log_value(h->log, 'M', 1, "Copy", "%4.1f%%", (t_copy / t_compute) * 100.0); oskar_log_value(h->log, 'M', 1, "Horizon clip", "%4.1f%%", (t_clip / t_compute) * 100.0); oskar_log_value(h->log, 'M', 1, "<NAME>", "%4.1f%%", (t_E / t_compute) * 100.0); oskar_log_value(h->log, 'M', 1, "<NAME>", "%4.1f%%", (t_K / t_compute) * 100.0); oskar_log_value(h->log, 'M', 1, "Jones join", "%4.1f%%", (t_join / t_compute) * 100.0); oskar_log_value(h->log, 'M', 1, "Jones correlate", "%4.1f%%", (t_correlate / t_compute) * 100.0); oskar_log_value(h->log, 'M', 1, "Other", "%4.1f%%", ((t_compute - t_components) / t_compute) * 100.0); free(compute_times); } static unsigned int disp_width(unsigned int v) { return (v >= 100000u) ? 6 : (v >= 10000u) ? 5 : (v >= 1000u) ? 4 : (v >= 100u) ? 3 : (v >= 10u) ? 2u : 1u; /* return v == 1u ? 1u : (unsigned)log10(v)+1 */ } static void system_mem_log(oskar_Log* log) { size_t mem_total, mem_free, mem_used, gigabyte = 1024 * 1024 * 1024; size_t mem_resident; mem_total = oskar_get_total_physical_memory(); mem_resident = oskar_get_memory_usage(); mem_free = oskar_get_free_physical_memory(); mem_used = mem_total - mem_free; oskar_log_message(log, 'M', 0, "System memory usage %.1f%% " "(%.1f GB/%.1f GB) used.", 100. * (double) mem_used / mem_total, (double) mem_used / gigabyte, (double) mem_total / gigabyte); oskar_log_message(log, 'M', 0, "Memory used by simulator: %.1f MB", (double) mem_resident / (1024. * 1024.)); } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/math/oskar_evaluate_image_lon_lat_grid.h
<reponame>davepallot/OSKAR /* * Copyright (c) 2013, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_EVALUATE_IMAGE_LON_LAT_GRID_H_ #define OSKAR_EVALUATE_IMAGE_LON_LAT_GRID_H_ /** * @file oskar_evaluate_image_lon_lat_grid.h */ #include <oskar_global.h> #include <mem/oskar_mem.h> #ifdef __cplusplus extern "C" { #endif /** * @brief * Generate longitude and latitude coordinates corresponding to image pixel * positions. * * @details * This function generates the longitude and latitude coordinates that * correspond to image pixel positions. * * This assumes an orthographic projection. * * @param[out] lon Output pixel positions in longitude, in radians. * @param[out] lat Output pixel positions in latitude, in radians. * @param[in] image_size_l Image side length in the l-dimension, in pixels. * @param[in] image_size_m Image side length in the m-dimension, in pixels. * @param[in] fov_rad_lon Field-of-view in longitude (l-dimension), in radians. * @param[in] fov_rad_lat Field-of-view in latitude (m-dimension), in radians. * @param[in] lon_rad Image centre longitude, in radians. * @param[in] lat_rad Image centre latitude, in radians. * @param[in] status Status return code. */ OSKAR_EXPORT void oskar_evaluate_image_lon_lat_grid(oskar_Mem* lon, oskar_Mem* lat, int image_size_l, int image_size_m, double fov_rad_lon, double fov_rad_lat, double lon_rad, double lat_rad, int* status); #ifdef __cplusplus } #endif #endif /* OSKAR_EVALUATE_IMAGE_LON_LAT_GRID_H_ */
davepallot/OSKAR
oskar/correlate/private_correlate_functions_inline.h
<gh_stars>1-10 /* * Copyright (c) 2013-2018, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_PRIVATE_CORRELATE_FUNCTIONS_INLINE_H_ #define OSKAR_PRIVATE_CORRELATE_FUNCTIONS_INLINE_H_ #include <oskar_global.h> #include <math/oskar_cmath.h> #include <math/oskar_multiply_inline.h> #define OMEGA_EARTH 7.272205217e-5 /* radians/sec */ #ifdef __cplusplus /* Evaluates sinc(x) = sin(x) / x. */ template <typename T> OSKAR_INLINE T oskar_sinc(const T x); template <> double oskar_sinc<double>(const double x) { return (x == 0.0) ? 1.0 : sin(x) / x; } template <> float oskar_sinc<float>(const float x) { return (x == 0.0f) ? 1.0f : sinf(x) / x; } #if defined __CUDA_ARCH__ && __CUDA_ARCH__ >= 300 #if CUDART_VERSION >= 9000 #define WARP_SHUFFLE_XOR(VAR, LANEMASK) __shfl_xor_sync(0xFFFFFFFF, VAR, LANEMASK) #else #define WARP_SHUFFLE_XOR(VAR, LANEMASK) __shfl_xor(VAR, LANEMASK) #endif #else #define WARP_SHUFFLE_XOR(VAR, LANEMASK) 0 #endif #define OSKAR_WARP_REDUCE(A) { \ (A) += WARP_SHUFFLE_XOR((A), 1); \ (A) += WARP_SHUFFLE_XOR((A), 2); \ (A) += WARP_SHUFFLE_XOR((A), 4); \ (A) += WARP_SHUFFLE_XOR((A), 8); \ (A) += WARP_SHUFFLE_XOR((A), 16); } #endif /* __cplusplus */ /* Evaluates sine and cosine of x. */ #ifdef __CUDACC__ #define OSKAR_SINCOS(REAL, X, S, C) sincos((REAL)X, &S, &C) #else #define OSKAR_SINCOS(REAL, X, S, C) S = sin((REAL)X); C = cos((REAL)X) #endif /* Evaluate various per-baseline terms. */ #define OSKAR_BASELINE_TERMS(REAL, S_UP, S_UQ, S_VP, S_VQ, S_WP, S_WQ, UU, VV, WW, UU2, VV2, UUVV, UV_LEN) { \ UU = (S_UP - S_UQ) * inv_wavelength; \ VV = (S_VP - S_VQ) * inv_wavelength; \ WW = (S_WP - S_WQ) * inv_wavelength; \ UU2 = UU * UU; VV2 = VV * VV; \ UV_LEN = sqrt((REAL) (UU2 + VV2)); \ UUVV = 2 * UU * VV; \ const REAL f = ((REAL) M_PI) * frac_bandwidth; \ UU *= f; VV *= f; WW *= f; } /* Evaluate baseline deltas for time-average smearing. */ #define OSKAR_BASELINE_DELTAS(REAL, S_XP, S_XQ, S_YP, S_YQ, DU, DV, DW) { \ REAL xx, yy, rot_angle, temp, sin_HA, cos_HA, sin_Dec, cos_Dec; \ OSKAR_SINCOS(REAL, gha0_rad, sin_HA, cos_HA); \ OSKAR_SINCOS(REAL, dec0_rad, sin_Dec, cos_Dec); \ temp = ((REAL) M_PI) * inv_wavelength; \ xx = (S_XP - S_XQ) * temp; \ yy = (S_YP - S_YQ) * temp; \ rot_angle = ((REAL) OMEGA_EARTH) * time_int_sec; \ temp = (xx * sin_HA + yy * cos_HA) * rot_angle; \ DU = (xx * cos_HA - yy * sin_HA) * rot_angle; \ DV = temp * sin_Dec; \ DW = -temp * cos_Dec; } /* Clears a complex matrix. */ #define OSKAR_CLEAR_COMPLEX_MATRIX(REAL, M) { \ M.a.x = M.a.y = M.b.x = M.b.y = (REAL)0; \ M.c.x = M.c.y = M.d.x = M.d.y = (REAL)0; } /* Construct source brightness matrix (ignoring c, as it's Hermitian). */ #define OSKAR_CONSTRUCT_B(REAL, B, SRC_I, SRC_Q, SRC_U, SRC_V) { \ REAL s_I__, s_Q__; s_I__ = SRC_I; s_Q__ = SRC_Q; \ B.b.x = SRC_U; B.b.y = SRC_V; \ B.a.x = s_I__ + s_Q__; B.d.x = s_I__ - s_Q__; } #if defined(__CUDACC__) && __CUDA_ARCH__ >= 350 /* Uses __ldg() instruction for global load via texture cache. */ /* Available only from CUDA architecture 3.5. */ #define OSKAR_LOAD_MATRIX(M, IND8) { \ M.a = __ldg(&(IND8.a)); \ M.b = __ldg(&(IND8.b)); \ M.c = __ldg(&(IND8.c)); \ M.d = __ldg(&(IND8.d)); } #else #define OSKAR_LOAD_MATRIX(M, IND8) M = IND8; #endif /* Evaluates a 1D linear baseline index for stations p and q. */ OSKAR_INLINE int oskar_evaluate_baseline_index_inline(const int num_stations, const int p, const int q) { return q * (num_stations - 1) - (q - 1) * q / 2 + p - q - 1; } #endif /* OSKAR_PRIVATE_CORRELATE_FUNCTIONS_INLINE_H_ */
davepallot/OSKAR
oskar/imager/src/private_imager_read_dims.c
<reponame>davepallot/OSKAR<gh_stars>1-10 /* * Copyright (c) 2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "imager/private_imager.h" #include "imager/private_imager_read_dims.h" #include "imager/oskar_imager.h" #include "binary/oskar_binary.h" #include "ms/oskar_measurement_set.h" #include "vis/oskar_vis_header.h" #ifdef __cplusplus extern "C" { #endif void oskar_imager_read_dims_ms(oskar_Imager* h, const char* filename, int* status) { #ifndef OSKAR_NO_MS oskar_MeasurementSet* ms; if (*status) return; /* Read the header. */ ms = oskar_ms_open(filename); if (!ms) { *status = OSKAR_ERR_FILE_IO; return; } /* Set visibility meta-data. */ oskar_imager_set_vis_frequency(h, oskar_ms_freq_start_hz(ms), oskar_ms_freq_inc_hz(ms), (int) oskar_ms_num_channels(ms)); oskar_ms_close(ms); #else (void) filename; oskar_log_error(h->log, "OSKAR was compiled without Measurement Set support."); *status = OSKAR_ERR_FUNCTION_NOT_AVAILABLE; #endif } void oskar_imager_read_dims_vis(oskar_Imager* h, const char* filename, int* status) { oskar_Binary* vis_file; oskar_VisHeader* header; if (*status) return; /* Read the header. */ vis_file = oskar_binary_create(filename, 'r', status); header = oskar_vis_header_read(vis_file, status); if (*status) { oskar_vis_header_free(header, status); oskar_binary_free(vis_file); return; } /* Set visibility meta-data. */ oskar_imager_set_vis_frequency(h, oskar_vis_header_freq_start_hz(header), oskar_vis_header_freq_inc_hz(header), oskar_vis_header_num_channels_total(header)); oskar_vis_header_free(header, status); oskar_binary_free(vis_file); } #ifdef __cplusplus } #endif
davepallot/OSKAR
gui/oskar_SettingsDelegate.h
/* * Copyright (c) 2012-2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_SETTINGS_DELEGATE_H_ #define OSKAR_SETTINGS_DELEGATE_H_ #include <QtWidgets/QStyledItemDelegate> class QModelIndex; class QWidget; namespace oskar { class SettingsDelegate : public QStyledItemDelegate { Q_OBJECT public: SettingsDelegate(QWidget* view, QObject* parent = 0); QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const; bool editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index); void setEditorData(QWidget* editor, const QModelIndex& index) const; void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const; void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const; private slots: void commitAndCloseEditor(int index); private: QWidget* view_; }; } /* namespace oskar */ #endif /* OSKAR_SETTINGS_DELEGATE_H_ */
davepallot/OSKAR
oskar/convert/src/oskar_convert_cirs_relative_directions_to_enu_directions.c
/* * Copyright (c) 2014-2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "convert/oskar_convert_cirs_relative_directions_to_enu_directions.h" #include "convert/oskar_convert_cirs_relative_directions_to_enu_directions_cuda.h" #include "convert/private_convert_cirs_relative_directions_to_enu_directions_inline.h" #include "convert/private_evaluate_cirs_observed_parameters.h" #ifdef __cplusplus extern "C" { #endif /* Single precision. */ void oskar_convert_cirs_relative_directions_to_enu_directions_f( int num_points, const float* l, const float* m, const float* n, float ra0_rad, float dec0_rad, float lon_rad, float lat_rad, float era_rad, float pm_x_rad, float pm_y_rad, float diurnal_aberration, float* x, float* y, float* z) { int i; double sin_lat, cos_lat, sin_ha0, cos_ha0, sin_dec0, cos_dec0; double local_pm_x, local_pm_y; /* Calculate common transform parameters. */ oskar_evaluate_cirs_observed_parameters(lon_rad, lat_rad, era_rad, ra0_rad, dec0_rad, pm_x_rad, pm_y_rad, &sin_lat, &cos_lat, &sin_ha0, &cos_ha0, &sin_dec0, &cos_dec0, &local_pm_x, &local_pm_y); /* Loop over positions. */ for (i = 0; i < num_points; ++i) { oskar_convert_cirs_relative_directions_to_enu_directions_inline_f( l[i], m[i], n[i], sin_lat, cos_lat, sin_ha0, cos_ha0, sin_dec0, cos_dec0, local_pm_x, local_pm_y, diurnal_aberration, &x[i], &y[i], &z[i]); } } /* Double precision. */ void oskar_convert_cirs_relative_directions_to_enu_directions_d( int num_points, const double* l, const double* m, const double* n, double ra0_rad, double dec0_rad, double lon_rad, double lat_rad, double era_rad, double pm_x_rad, double pm_y_rad, double diurnal_aberration, double* x, double* y, double* z) { int i; double sin_lat, cos_lat, sin_ha0, cos_ha0, sin_dec0, cos_dec0; double local_pm_x, local_pm_y; /* Calculate common transform parameters. */ oskar_evaluate_cirs_observed_parameters(lon_rad, lat_rad, era_rad, ra0_rad, dec0_rad, pm_x_rad, pm_y_rad, &sin_lat, &cos_lat, &sin_ha0, &cos_ha0, &sin_dec0, &cos_dec0, &local_pm_x, &local_pm_y); /* Loop over positions. */ for (i = 0; i < num_points; ++i) { oskar_convert_cirs_relative_directions_to_enu_directions_inline_d( l[i], m[i], n[i], sin_lat, cos_lat, sin_ha0, cos_ha0, sin_dec0, cos_dec0, local_pm_x, local_pm_y, diurnal_aberration, &x[i], &y[i], &z[i]); } } /* Wrapper. */ void oskar_convert_cirs_relative_directions_to_enu_directions(int num_points, const oskar_Mem* l, const oskar_Mem* m, const oskar_Mem* n, double ra0_rad, double dec0_rad, double lon_rad, double lat_rad, double era_rad, double pm_x_rad, double pm_y_rad, double diurnal_aberration, oskar_Mem* x, oskar_Mem* y, oskar_Mem* z, int* status) { int type, location; /* Check if safe to proceed. */ if (*status) return; /* Get type and check consistency. */ type = oskar_mem_type(x); if (type != OSKAR_SINGLE && type != OSKAR_DOUBLE) { *status = OSKAR_ERR_BAD_DATA_TYPE; return; } if (type != oskar_mem_type(y) || type != oskar_mem_type(z) || type != oskar_mem_type(l) || type != oskar_mem_type(m) || type != oskar_mem_type(n)) { *status = OSKAR_ERR_TYPE_MISMATCH; return; } /* Get location and check consistency. */ location = oskar_mem_location(x); if (location != oskar_mem_location(y) || location != oskar_mem_location(z) || location != oskar_mem_location(l) || location != oskar_mem_location(m) || location != oskar_mem_location(n)) { *status = OSKAR_ERR_LOCATION_MISMATCH; return; } /* Check dimension consistency. */ if ((int)oskar_mem_length(x) < num_points || (int)oskar_mem_length(y) < num_points || (int)oskar_mem_length(z) < num_points || (int)oskar_mem_length(l) < num_points || (int)oskar_mem_length(m) < num_points || (int)oskar_mem_length(n) < num_points) { *status = OSKAR_ERR_DIMENSION_MISMATCH; return; } /* Switch on type and location. */ if (type == OSKAR_DOUBLE) { double *x_, *y_, *z_; const double *l_, *m_, *n_; x_ = oskar_mem_double(x, status); y_ = oskar_mem_double(y, status); z_ = oskar_mem_double(z, status); l_ = oskar_mem_double_const(l, status); m_ = oskar_mem_double_const(m, status); n_ = oskar_mem_double_const(n, status); if (location == OSKAR_CPU) { oskar_convert_cirs_relative_directions_to_enu_directions_d( num_points, l_, m_, n_, ra0_rad, dec0_rad, lon_rad, lat_rad, era_rad, pm_x_rad, pm_y_rad, diurnal_aberration, x_, y_, z_); } else if (location == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA oskar_convert_cirs_relative_directions_to_enu_directions_cuda_d( num_points, l_, m_, n_, ra0_rad, dec0_rad, lon_rad, lat_rad, era_rad, pm_x_rad, pm_y_rad, diurnal_aberration, x_, y_, z_); #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } } else { float *x_, *y_, *z_; const float *l_, *m_, *n_; x_ = oskar_mem_float(x, status); y_ = oskar_mem_float(y, status); z_ = oskar_mem_float(z, status); l_ = oskar_mem_float_const(l, status); m_ = oskar_mem_float_const(m, status); n_ = oskar_mem_float_const(n, status); if (location == OSKAR_CPU) { oskar_convert_cirs_relative_directions_to_enu_directions_f( num_points, l_, m_, n_, ra0_rad, dec0_rad, lon_rad, lat_rad, era_rad, pm_x_rad, pm_y_rad, diurnal_aberration, x_, y_, z_); } else if (location == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA oskar_convert_cirs_relative_directions_to_enu_directions_cuda_f( num_points, l_, m_, n_, ra0_rad, dec0_rad, lon_rad, lat_rad, era_rad, pm_x_rad, pm_y_rad, diurnal_aberration, x_, y_, z_); #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } } } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/mem/oskar_mem_save_ascii.h
/* * Copyright (c) 2013, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_MEM_SAVE_ASCII_H_ #define OSKAR_MEM_SAVE_ASCII_H_ /** * @file oskar_mem_save_ascii.h */ #include <oskar_global.h> #include <stdio.h> #ifdef __cplusplus extern "C" { #endif /** * @brief * Saves the given blocks of memory to an ASCII table. * * @details * This function saves the given blocks of memory to an ASCII table using the * specified stream. * * The variable argument list must contain pointers to oskar_Mem structures. * Data within these structures may reside either in CPU or GPU memory. * The number of structures passed is given by the \p num_mem parameter. * * All structures must contain at least the number of specified * \p num_elements. Each array will form one (or more, if using complex types) * columns of the output table, with the row corresponding to the element * index. * * @param[in] file Pointer to output stream. * @param[in] num_mem Number of arrays to write. * @param[in] num_elements Number of elements to write. * @param[in,out] status Status return code. */ OSKAR_EXPORT void oskar_mem_save_ascii(FILE* file, size_t num_mem, size_t num_elements, int* status, ...); #ifdef __cplusplus } #endif #endif /* OSKAR_MEM_SAVE_ASCII_H_ */
davepallot/OSKAR
oskar/telescope/station/element/oskar_element_accessors.h
/* * Copyright (c) 2013-2014, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_ELEMENT_ACCESSORS_H_ #define OSKAR_ELEMENT_ACCESSORS_H_ /** * @file oskar_element_accessors.h */ #include <oskar_global.h> #include <mem/oskar_mem.h> #include <splines/oskar_splines.h> #ifdef __cplusplus extern "C" { #endif OSKAR_EXPORT int oskar_element_precision(const oskar_Element* data); OSKAR_EXPORT int oskar_element_mem_location(const oskar_Element* data); OSKAR_EXPORT int oskar_element_has_x_spline_data(const oskar_Element* data); OSKAR_EXPORT int oskar_element_has_y_spline_data(const oskar_Element* data); OSKAR_EXPORT int oskar_element_has_scalar_spline_data(const oskar_Element* data); OSKAR_EXPORT int oskar_element_num_freq(const oskar_Element* data); OSKAR_EXPORT const double* oskar_element_freqs_hz_const(const oskar_Element* data); OSKAR_EXPORT int oskar_element_type(const oskar_Element* data); OSKAR_EXPORT int oskar_element_taper_type(const oskar_Element* data); OSKAR_EXPORT double oskar_element_cosine_power(const oskar_Element* data); OSKAR_EXPORT double oskar_element_gaussian_fwhm_rad(const oskar_Element* data); OSKAR_EXPORT double oskar_element_dipole_length(const oskar_Element* data); OSKAR_EXPORT int oskar_element_dipole_length_units(const oskar_Element* data); OSKAR_EXPORT oskar_Mem* oskar_element_x_filename(oskar_Element* data, int freq_id); OSKAR_EXPORT const oskar_Mem* oskar_element_x_filename_const(const oskar_Element* data, int freq_id); OSKAR_EXPORT oskar_Mem* oskar_element_y_filename(oskar_Element* data, int freq_id); OSKAR_EXPORT const oskar_Mem* oskar_element_y_filename_const(const oskar_Element* data, int freq_id); OSKAR_EXPORT oskar_Mem* oskar_element_scalar_filename(oskar_Element* data, int freq_id); OSKAR_EXPORT const oskar_Mem* oskar_element_scalar_filename_const(const oskar_Element* data, int freq_id); OSKAR_EXPORT oskar_Splines* oskar_element_x_h_re(oskar_Element* data, int freq_id); OSKAR_EXPORT const oskar_Splines* oskar_element_x_h_re_const(const oskar_Element* data, int freq_id); OSKAR_EXPORT oskar_Splines* oskar_element_x_h_im(oskar_Element* data, int freq_id); OSKAR_EXPORT const oskar_Splines* oskar_element_x_h_im_const(const oskar_Element* data, int freq_id); OSKAR_EXPORT oskar_Splines* oskar_element_x_v_re(oskar_Element* data, int freq_id); OSKAR_EXPORT const oskar_Splines* oskar_element_x_v_re_const(const oskar_Element* data, int freq_id); OSKAR_EXPORT oskar_Splines* oskar_element_x_v_im(oskar_Element* data, int freq_id); OSKAR_EXPORT const oskar_Splines* oskar_element_x_v_im_const(const oskar_Element* data, int freq_id); OSKAR_EXPORT oskar_Splines* oskar_element_y_h_re(oskar_Element* data, int freq_id); OSKAR_EXPORT const oskar_Splines* oskar_element_y_h_re_const(const oskar_Element* data, int freq_id); OSKAR_EXPORT oskar_Splines* oskar_element_y_h_im(oskar_Element* data, int freq_id); OSKAR_EXPORT const oskar_Splines* oskar_element_y_h_im_const(const oskar_Element* data, int freq_id); OSKAR_EXPORT oskar_Splines* oskar_element_y_v_re(oskar_Element* data, int freq_id); OSKAR_EXPORT const oskar_Splines* oskar_element_y_v_re_const(const oskar_Element* data, int freq_id); OSKAR_EXPORT oskar_Splines* oskar_element_y_v_im(oskar_Element* data, int freq_id); OSKAR_EXPORT const oskar_Splines* oskar_element_y_v_im_const(const oskar_Element* data, int freq_id); OSKAR_EXPORT oskar_Splines* oskar_element_scalar_re(oskar_Element* data, int freq_id); OSKAR_EXPORT const oskar_Splines* oskar_element_scalar_re_const(const oskar_Element* data, int freq_id); OSKAR_EXPORT oskar_Splines* oskar_element_scalar_im(oskar_Element* data, int freq_id); OSKAR_EXPORT const oskar_Splines* oskar_element_scalar_im_const(const oskar_Element* data, int freq_id); /* Setters. */ OSKAR_EXPORT void oskar_element_set_element_type(oskar_Element* data, const char* type, int* status); OSKAR_EXPORT void oskar_element_set_taper_type(oskar_Element* data, const char* type, int* status); OSKAR_EXPORT void oskar_element_set_gaussian_fwhm_rad(oskar_Element* data, double value); OSKAR_EXPORT void oskar_element_set_cosine_power(oskar_Element* data, double value); OSKAR_EXPORT void oskar_element_set_dipole_length(oskar_Element* data, double value, const char* units, int* status); #ifdef __cplusplus } #endif #endif /* OSKAR_ELEMENT_ACCESSORS_H_ */
davepallot/OSKAR
oskar/utility/oskar_cl_utils.h
<reponame>davepallot/OSKAR /* * Copyright (c) 2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include <oskar_global.h> #ifdef OSKAR_HAVE_OPENCL #define CL_USE_DEPRECATED_OPENCL_1_2_APIS #ifdef __APPLE__ #include <OpenCL/opencl.h> #else #include <CL/cl.h> #endif #endif #ifdef __cplusplus extern "C" { #endif #ifdef OSKAR_HAVE_OPENCL OSKAR_EXPORT cl_command_queue oskar_cl_command_queue(void); OSKAR_EXPORT cl_context oskar_cl_context(void); OSKAR_EXPORT cl_device_id oskar_cl_device_id(void); OSKAR_EXPORT cl_kernel oskar_cl_kernel(const char*); #endif OSKAR_EXPORT const char* oskar_cl_device_cl_version(void); OSKAR_EXPORT const char* oskar_cl_device_driver_version(void); OSKAR_EXPORT const char* oskar_cl_device_name(void); OSKAR_EXPORT void oskar_cl_free(void); OSKAR_EXPORT unsigned int oskar_cl_get_device(void); /** * @brief * Initialises OpenCL contexts and command queues. * * @details * Creates contexts and command queues to work with OpenCL devices * matching the supplied parameters. * * The device type is optional. If not specified, all devices are selected. * * The device vendor name is optional. * More than one vendor name may be given in the same string using "|" or "," * as a delimiter. * * If either parameter is NULL, the values are checked from the * environment variables OSKAR_CL_DEVICE_TYPE and OSKAR_CL_DEVICE_VENDOR. * * @param[in] device_type String containing required device type * (GPU, CPU or Accelerator). May be NULL. * @param[in] device_vendor String containing required device vendor name. * May be NULL. */ OSKAR_EXPORT void oskar_cl_init(const char* device_type, const char* device_vendor); OSKAR_EXPORT int oskar_cl_is_gpu(void); OSKAR_EXPORT unsigned int oskar_cl_num_devices(void); OSKAR_EXPORT void oskar_cl_set_device(unsigned int device, int* status); #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/mem/src/oskar_mem_get_element.c
/* * Copyright (c) 2014-2016, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifdef OSKAR_HAVE_CUDA #include <cuda_runtime_api.h> #endif #include "mem/oskar_mem.h" #include "mem/private_mem.h" #ifdef __cplusplus extern "C" { #endif double oskar_mem_get_element(const oskar_Mem* mem, size_t index, int* status) { int precision, location; /* Check if safe to proceed. */ if (*status) return 0.0; /* Get the data precision and location. */ precision = oskar_type_precision(mem->type); location = mem->location; /* Get the data. */ if (location == OSKAR_CPU) { switch (precision) { case OSKAR_DOUBLE: return ((double*)(mem->data))[index]; case OSKAR_SINGLE: return ((float*)(mem->data))[index]; default: *status = OSKAR_ERR_BAD_DATA_TYPE; } } else if (location == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA switch (precision) { case OSKAR_DOUBLE: { double val; cudaMemcpy(&val, (double*)(mem->data) + index, sizeof(double), cudaMemcpyDeviceToHost); return val; } case OSKAR_SINGLE: { float val; cudaMemcpy(&val, (float*)(mem->data) + index, sizeof(float), cudaMemcpyDeviceToHost); return val; } default: *status = OSKAR_ERR_BAD_DATA_TYPE; } #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } else *status = OSKAR_ERR_BAD_LOCATION; return 0.0; } double2 oskar_mem_get_element_complex(const oskar_Mem* mem, size_t index, int* status) { size_t n; int type, location; double2 val; val.x = 0.0; val.y = 0.0; /* Check if safe to proceed. */ if (*status) return val; /* Get the data type, location, and number of elements. */ type = mem->type; location = mem->location; n = mem->num_elements; if (index >= n) { *status = OSKAR_ERR_OUT_OF_RANGE; return val; } /* Get the data. */ if (location == OSKAR_CPU) { if (type == OSKAR_DOUBLE_COMPLEX) { return ((double2*)(mem->data))[index]; } else if (type == OSKAR_SINGLE_COMPLEX) { float2 temp; temp = ((float2*)(mem->data))[index]; val.x = (double) temp.x; val.y = (double) temp.y; } else *status = OSKAR_ERR_BAD_DATA_TYPE; } else if (location == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA if (type == OSKAR_DOUBLE_COMPLEX) { cudaMemcpy(&val, (double2*)(mem->data) + index, sizeof(double2), cudaMemcpyDeviceToHost); return val; } else if (type == OSKAR_SINGLE_COMPLEX) { float2 temp; cudaMemcpy(&temp, (float2*)(mem->data) + index, sizeof(float2), cudaMemcpyDeviceToHost); val.x = (double) temp.x; val.y = (double) temp.y; return val; } else *status = OSKAR_ERR_BAD_DATA_TYPE; #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } else *status = OSKAR_ERR_BAD_LOCATION; return val; } double4c oskar_mem_get_element_matrix(const oskar_Mem* mem, size_t index, int* status) { size_t n; int type, location; double4c val; val.a.x = 0.0; val.a.y = 0.0; val.b.x = 0.0; val.b.y = 0.0; val.c.x = 0.0; val.c.y = 0.0; val.d.x = 0.0; val.d.y = 0.0; /* Check if safe to proceed. */ if (*status) return val; /* Get the data type, location, and number of elements. */ type = mem->type; location = mem->location; n = mem->num_elements; if (index >= n) { *status = OSKAR_ERR_OUT_OF_RANGE; return val; } /* Get the data. */ if (location == OSKAR_CPU) { if (type == OSKAR_DOUBLE_COMPLEX_MATRIX) { return ((double4c*)(mem->data))[index]; } else if (type == OSKAR_SINGLE_COMPLEX_MATRIX) { float4c temp; temp = ((float4c*)(mem->data))[index]; val.a.x = (double) temp.a.x; val.a.y = (double) temp.a.y; val.b.x = (double) temp.b.x; val.b.y = (double) temp.b.y; val.c.x = (double) temp.c.x; val.c.y = (double) temp.c.y; val.d.x = (double) temp.d.x; val.d.y = (double) temp.d.y; return val; } else *status = OSKAR_ERR_BAD_DATA_TYPE; } else if (location == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA if (type == OSKAR_DOUBLE_COMPLEX_MATRIX) { cudaMemcpy(&val, (double4c*)(mem->data) + index, sizeof(double4c), cudaMemcpyDeviceToHost); return val; } else if (type == OSKAR_SINGLE_COMPLEX_MATRIX) { float4c temp; cudaMemcpy(&temp, (float4c*)(mem->data) + index, sizeof(float4c), cudaMemcpyDeviceToHost); val.a.x = (double) temp.a.x; val.a.y = (double) temp.a.y; val.b.x = (double) temp.b.x; val.b.y = (double) temp.b.y; val.c.x = (double) temp.c.x; val.c.y = (double) temp.c.y; val.d.x = (double) temp.d.x; val.d.y = (double) temp.d.y; return val; } else *status = OSKAR_ERR_BAD_DATA_TYPE; #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } else *status = OSKAR_ERR_BAD_LOCATION; return val; } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/convert/src/oskar_convert_enu_directions_to_relative_directions.c
/* * Copyright (c) 2013-2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "convert/oskar_convert_enu_directions_to_relative_directions.h" #include "convert/oskar_convert_enu_directions_to_relative_directions_cuda.h" #include "convert/oskar_convert_enu_directions_to_relative_directions_inline.h" #include <math.h> #ifdef __cplusplus extern "C" { #endif /* Single precision. */ void oskar_convert_enu_directions_to_relative_directions_f( float* l, float* m, float* n, int num_points, const float* x, const float* y, const float* z, float ha0, float dec0, float lat) { int i; float sin_ha0, cos_ha0, sin_dec0, cos_dec0, sin_lat, cos_lat; sin_ha0 = (float) sin(ha0); cos_ha0 = (float) cos(ha0); sin_dec0 = (float) sin(dec0); cos_dec0 = (float) cos(dec0); sin_lat = (float) sin(lat); cos_lat = (float) cos(lat); for (i = 0; i < num_points; ++i) { oskar_convert_enu_directions_to_relative_directions_inline_f( &l[i], &m[i], &n[i], x[i], y[i], z[i], cos_ha0, sin_ha0, cos_dec0, sin_dec0, cos_lat, sin_lat); } } /* Double precision. */ void oskar_convert_enu_directions_to_relative_directions_d( double* l, double* m, double* n, int num_points, const double* x, const double* y, const double* z, double ha0, double dec0, double lat) { int i; double sin_ha0, cos_ha0, sin_dec0, cos_dec0, sin_lat, cos_lat; sin_ha0 = sin(ha0); cos_ha0 = cos(ha0); sin_dec0 = sin(dec0); cos_dec0 = cos(dec0); sin_lat = sin(lat); cos_lat = cos(lat); for (i = 0; i < num_points; ++i) { oskar_convert_enu_directions_to_relative_directions_inline_d( &l[i], &m[i], &n[i], x[i], y[i], z[i], cos_ha0, sin_ha0, cos_dec0, sin_dec0, cos_lat, sin_lat); } } /* Wrapper. */ void oskar_convert_enu_directions_to_relative_directions( oskar_Mem* l, oskar_Mem* m, oskar_Mem* n, int num_points, const oskar_Mem* x, const oskar_Mem* y, const oskar_Mem* z, double ha0, double dec0, double lat, int* status) { int type, location; /* Check if safe to proceed. */ if (*status) return; /* Get type and check consistency. */ type = oskar_mem_type(x); if (type != OSKAR_SINGLE && type != OSKAR_DOUBLE) { *status = OSKAR_ERR_BAD_DATA_TYPE; return; } if (type != oskar_mem_type(y) || type != oskar_mem_type(z) || type != oskar_mem_type(l) || type != oskar_mem_type(m) || type != oskar_mem_type(n)) { *status = OSKAR_ERR_TYPE_MISMATCH; return; } /* Get location and check consistency. */ location = oskar_mem_location(x); if (location != oskar_mem_location(y) || location != oskar_mem_location(z) || location != oskar_mem_location(l) || location != oskar_mem_location(m) || location != oskar_mem_location(n)) { *status = OSKAR_ERR_LOCATION_MISMATCH; return; } /* Check dimension consistency. */ if ((int)oskar_mem_length(x) < num_points || (int)oskar_mem_length(y) < num_points || (int)oskar_mem_length(z) < num_points || (int)oskar_mem_length(l) < num_points || (int)oskar_mem_length(m) < num_points || (int)oskar_mem_length(n) < num_points) { *status = OSKAR_ERR_DIMENSION_MISMATCH; return; } /* Switch on type and location. */ if (type == OSKAR_DOUBLE) { double *l_, *m_, *n_; const double *x_, *y_, *z_; l_ = oskar_mem_double(l, status); m_ = oskar_mem_double(m, status); n_ = oskar_mem_double(n, status); x_ = oskar_mem_double_const(x, status); y_ = oskar_mem_double_const(y, status); z_ = oskar_mem_double_const(z, status); if (location == OSKAR_CPU) { oskar_convert_enu_directions_to_relative_directions_d( l_, m_, n_, num_points, x_, y_, z_, ha0, dec0, lat); } else if (location == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA oskar_convert_enu_directions_to_relative_directions_cuda_d( l_, m_, n_, num_points, x_, y_, z_, ha0, dec0, lat); #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } } else { float *l_, *m_, *n_; const float *x_, *y_, *z_; l_ = oskar_mem_float(l, status); m_ = oskar_mem_float(m, status); n_ = oskar_mem_float(n, status); x_ = oskar_mem_float_const(x, status); y_ = oskar_mem_float_const(y, status); z_ = oskar_mem_float_const(z, status); if (location == OSKAR_CPU) { oskar_convert_enu_directions_to_relative_directions_f( l_, m_, n_, num_points, x_, y_, z_, ha0, dec0, lat); } else if (location == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA oskar_convert_enu_directions_to_relative_directions_cuda_f( l_, m_, n_, num_points, x_, y_, z_, ha0, dec0, lat); #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } } } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/telescope/station/oskar_evaluate_beam_horizon_direction.h
<reponame>davepallot/OSKAR /* * Copyright (c) 2011-2013, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_EVALUATE_BEAM_HORIZON_DIRECTION_H_ #define OSKAR_EVALUATE_BEAM_HORIZON_DIRECTION_H_ /** * @file oskar_evaluate_beam_horizon_direction.h */ #include <oskar_global.h> #include <telescope/station/oskar_station.h> #ifdef __cplusplus extern "C" { #endif /** * @brief Wrapper function to evaluate the beam phase centre coordinates as * horizontal direction cosines for the specified Greenwich apparent sidereal * time. * * @details * Converts the station beam phase centre from equatorial (RA, Dec) coordinates * stored in the station model to horizontal lmn coordinates stored in the * E-Jones work buffer for the specified time and the location of the * station as described by the longitude and latitude fields of the station * structure. * * @param[out] x Horizontal x-direction-cosine of the beam phase centre. * @param[out] y Horizontal y-direction-cosine of the beam phase centre. * @param[out] z Horizontal z-direction-cosine of the beam phase centre. * @param[in] station OSKAR Station structure. * @param[in] gast The Greenwich apparent sidereal time, in radians. * @param[in,out] status Status return code. */ OSKAR_EXPORT void oskar_evaluate_beam_horizon_direction(double* x, double* y, double* z, const oskar_Station* station, const double gast, int* status); #ifdef __cplusplus } #endif #endif /* OSKAR_EVALUATE_BEAM_HORIZON_DIRECTION_H_ */
davepallot/OSKAR
oskar/math/oskar_dftw.h
/* * Copyright (c) 2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_DFTW_H_ #define OSKAR_DFTW_H_ /** * @file oskar_dftw.h */ #include <oskar_global.h> #include <mem/oskar_mem.h> #ifdef __cplusplus extern "C" { #endif /** * @brief * Function to perform a DFT using supplied weights. * * @details * This function performs a DFT using the supplied weights array. * * The transform may be either 2D or 3D. If either \p z_in or \p z_out * is NULL on input, the transform will be done in 2D. * * The wavelength used to compute the supplied wavenumber must be in the * same units as the input positions (e.g. metres). * * If the \p data parameter is NULL, then all input values will * be implicitly assumed to be real, with value 1.0. If not NULL, * the \p data array must be complex and of size \p num_out * \p num_in. * It is accessed in such a way that the output dimension must be the * fastest varying. * * The computed points are returned in the \p output array. * These are the complex values for each output position. * * @param[in] num_in Number of input points. * @param[in] wavenumber Wavenumber (2 pi / wavelength). * @param[in] x_in Array of input x positions. * @param[in] y_in Array of input y positions. * @param[in] z_in Array of input z positions. * @param[in] weights_in Array of complex DFT weights. * @param[in] num_out Number of output points. * @param[in] x_out Array of output 1/x positions. * @param[in] y_out Array of output 1/y positions. * @param[in] z_out Array of output 1/z positions. * @param[out] data Input data (see note, above). * @param[out] output Array of computed output points (see note, above). * @param[in,out] status Status return code. */ OSKAR_EXPORT void oskar_dftw( int num_in, double wavenumber, const oskar_Mem* x_in, const oskar_Mem* y_in, const oskar_Mem* z_in, const oskar_Mem* weights_in, int num_out, const oskar_Mem* x_out, const oskar_Mem* y_out, const oskar_Mem* z_out, const oskar_Mem* data, oskar_Mem* output, int* status); #ifdef __cplusplus } #endif #endif
davepallot/OSKAR
oskar/math/oskar_dftw_m2m_2d_cuda.h
<gh_stars>1-10 /* * Copyright (c) 2012-2013, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_DFTW_M2M_2D_CUDA_H_ #define OSKAR_DFTW_M2M_2D_CUDA_H_ /** * @file oskar_dftw_m2m_2d_cuda.h */ #include <oskar_global.h> #include <utility/oskar_vector_types.h> #ifdef __cplusplus extern "C" { #endif /** * @brief * CUDA function to perform a 2D complex-matrix-to-complex-matrix * single-precision DFT using supplied weights. * * @details * This CUDA function performs a 2D complex-matrix-to-complex-matrix DFT using * the supplied complex weights and complex input data. * * The wavelength used to compute the supplied wavenumber must be in the * same units as the input positions. * * The input data must be supplied in an array of size \p n_out * \p n_in. * It is accessed in such a way that the output dimension must be the * fastest varying. * * The computed points are returned in the \p d_output array, which must be * pre-sized to length n_out. The values in the \p d_output array are * the complex values for each output position. * * Note that all pointers refer to device memory. * * @param[in] n_in Number of input points. * @param[in] wavenumber Wavenumber (2 pi / wavelength). * @param[in] d_x_in Array of input x positions. * @param[in] d_y_in Array of input y positions. * @param[in] d_weights_in Array of complex DFT weights. * @param[in] n_out Number of output points. * @param[in] d_x_out Array of output 1/x positions. * @param[in] d_y_out Array of output 1/y positions. * @param[in] d_data Array of complex input data (size n_out * n_in). * @param[out] d_output Array of computed output points (see note, above). */ OSKAR_EXPORT void oskar_dftw_m2m_2d_cuda_f(int n_in, float wavenumber, const float* d_x_in, const float* d_y_in, const float2* d_weights_in, int n_out, const float* d_x_out, const float* d_y_out, const float4c* d_data, float4c* d_output); /** * @brief * CUDA function to perform a 2D complex-matrix-to-complex-matrix * double-precision DFT using supplied weights. * * @details * This CUDA function performs a 2D complex-matrix-to-complex-matrix DFT using * the supplied complex weights and complex input data. * * The wavelength used to compute the supplied wavenumber must be in the * same units as the input positions. * * The input data must be supplied in an array of size \p n_out * \p n_in. * It is accessed in such a way that the output dimension must be the * fastest varying. * * The computed points are returned in the \p d_output array, which must be * pre-sized to length n_out. The values in the \p d_output array are * the complex values for each output position. * * Note that all pointers refer to device memory. * * @param[in] n_in Number of input points. * @param[in] wavenumber Wavenumber (2 pi / wavelength). * @param[in] d_x_in Array of input x positions. * @param[in] d_y_in Array of input y positions. * @param[in] d_weights_in Array of complex DFT weights. * @param[in] n_out Number of output points. * @param[in] d_x_out Array of output 1/x positions. * @param[in] d_y_out Array of output 1/y positions. * @param[in] d_data Array of complex input data (size n_out * n_in). * @param[out] d_output Array of computed output points (see note, above). */ OSKAR_EXPORT void oskar_dftw_m2m_2d_cuda_d(int n_in, double wavenumber, const double* d_x_in, const double* d_y_in, const double2* d_weights_in, int n_out, const double* d_x_out, const double* d_y_out, const double4c* d_data, double4c* d_output); #ifdef __cplusplus } #endif #endif /* OSKAR_DFTW_M2M_2D_CUDA_H_ */
davepallot/OSKAR
oskar/convert/oskar_convert_lon_lat_to_relative_directions_cuda.h
/* * Copyright (c) 2013-2014, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_CONVERT_LON_LAT_TO_RELATIVE_DIRECTIONS_CUDA_H_ #define OSKAR_CONVERT_LON_LAT_TO_RELATIVE_DIRECTIONS_CUDA_H_ /** * @file oskar_convert_lon_lat_to_relative_directions_cuda.h */ #include <oskar_global.h> #ifdef __cplusplus extern "C" { #endif /** * @brief * Spherical to relative 3D direction cosines (single precision). * * @details * This function computes the l,m,n direction cosines of the specified points * relative to the reference point. * * @param[in] num_points Number of points. * @param[in] d_lon_rad Input longitudes in radians. * @param[in] d_lat_rad Input latitudes in radians. * @param[in] lon0_rad Longitude of the reference point in radians. * @param[in] lat0_rad Latitude of the reference point in radians. * @param[out] d_l l-direction-cosines relative to the reference point. * @param[out] d_m m-direction-cosines relative to the reference point. * @param[out] d_n n-direction-cosines relative to the reference point. */ OSKAR_EXPORT void oskar_convert_lon_lat_to_relative_directions_cuda_f(int num_points, const float* d_lon_rad, const float* d_lat_rad, float lon0_rad, float lat0_rad, float* d_l, float* d_m, float* d_n); /** * @brief * Spherical to relative 3D direction cosines (double precision). * * @details * This function computes the l,m,n direction cosines of the specified points * relative to the reference point. * * @param[in] num_points Number of points. * @param[in] d_lon_rad Input longitudes in radians. * @param[in] d_lat_rad Input latitudes in radians. * @param[in] lon0_rad Longitude of the reference point in radians. * @param[in] lat0_rad Latitude of the reference point in radians. * @param[out] d_l l-direction-cosines relative to the reference point. * @param[out] d_m m-direction-cosines relative to the reference point. * @param[out] d_n n-direction-cosines relative to the reference point. */ OSKAR_EXPORT void oskar_convert_lon_lat_to_relative_directions_cuda_d(int num_points, const double* d_lon_rad, const double* d_lat_rad, double lon0_rad, double lat0_rad, double* d_l, double* d_m, double* d_n); #ifdef __CUDACC__ /* Kernels. */ __global__ void oskar_convert_lon_lat_to_relative_directions_cudak_f(const int num_points, const float* restrict lon_rad, const float* restrict lat_rad, const float lon0_rad, const float cos_lat0, const float sin_lat0, float* restrict l, float* restrict m, float* restrict n); __global__ void oskar_convert_lon_lat_to_relative_directions_cudak_d(const int num_points, const double* restrict lon_rad, const double* restrict lat_rad, const double lon0_rad, const double cos_lat0, const double sin_lat0, double* restrict l, double* restrict m, double* restrict n); #endif /* __CUDACC__ */ #ifdef __cplusplus } #endif #endif /* OSKAR_CONVERT_LON_LAT_TO_RELATIVE_DIRECTIONS_CUDA_H_ */
davepallot/OSKAR
oskar/convert/src/oskar_convert_ecef_to_baseline_uvw.c
<gh_stars>1-10 /* * Copyright (c) 2013-2016, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "convert/oskar_convert_ecef_to_baseline_uvw.h" #include "convert/oskar_convert_station_uvw_to_baseline_uvw.h" #include "convert/oskar_convert_ecef_to_station_uvw.h" #include "convert/oskar_convert_mjd_to_gast_fast.h" #ifdef __cplusplus extern "C" { #endif void oskar_convert_ecef_to_baseline_uvw(int num_stations, const oskar_Mem* x, const oskar_Mem* y, const oskar_Mem* z, double ra0_rad, double dec0_rad, int num_times, double time_ref_mjd_utc, double time_inc_days, int start_time_index, oskar_Mem* uu, oskar_Mem* vv, oskar_Mem* ww, oskar_Mem* work, int* status) { oskar_Mem *u, *v, *w, *uu_dump, *vv_dump, *ww_dump; /* Aliases. */ int i, num_baselines; /* Check if safe to proceed. */ if (*status) return; /* Check that the data dimensions are OK. */ num_baselines = num_stations * (num_stations - 1) / 2; if ((int)oskar_mem_length(uu) < num_baselines * num_times || (int)oskar_mem_length(vv) < num_baselines * num_times || (int)oskar_mem_length(ww) < num_baselines * num_times || (int)oskar_mem_length(x) < num_stations || (int)oskar_mem_length(y) < num_stations || (int)oskar_mem_length(z) < num_stations) { *status = OSKAR_ERR_DIMENSION_MISMATCH; return; } if ((int)oskar_mem_length(work) < 3 * num_stations) oskar_mem_realloc(work, 3 * num_stations, status); if (*status) return; /* Create pointers from work buffer. */ u = oskar_mem_create_alias(work, 0, num_stations, status); v = oskar_mem_create_alias(work, 1 * num_stations, num_stations, status); w = oskar_mem_create_alias(work, 2 * num_stations, num_stations, status); /* Create pointers to baseline u,v,w coordinates for dump. */ uu_dump = oskar_mem_create_alias(0, 0, 0, status); vv_dump = oskar_mem_create_alias(0, 0, 0, status); ww_dump = oskar_mem_create_alias(0, 0, 0, status); /* Loop over times. */ for (i = 0; i < num_times; ++i) { double t_dump, gast; t_dump = time_ref_mjd_utc + time_inc_days * ((i + 0.5) + start_time_index); gast = oskar_convert_mjd_to_gast_fast(t_dump); /* Compute u,v,w coordinates of mid point. */ oskar_convert_ecef_to_station_uvw(num_stations, x, y, z, ra0_rad, dec0_rad, gast, u, v, w, status); /* Extract pointers to baseline u,v,w coordinates for this dump. */ oskar_mem_set_alias(uu_dump, uu, i * num_baselines, num_baselines, status); oskar_mem_set_alias(vv_dump, vv, i * num_baselines, num_baselines, status); oskar_mem_set_alias(ww_dump, ww, i * num_baselines, num_baselines, status); /* Compute baselines from station positions. */ oskar_convert_station_uvw_to_baseline_uvw(u, v, w, uu_dump, vv_dump, ww_dump, status); } /* Free handles to aliased memory. */ oskar_mem_free(u, status); oskar_mem_free(v, status); oskar_mem_free(w, status); oskar_mem_free(uu_dump, status); oskar_mem_free(vv_dump, status); oskar_mem_free(ww_dump, status); } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/mem/oskar_mem.h
<reponame>davepallot/OSKAR /* * Copyright (c) 2012-2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_MEM_H_ #define OSKAR_MEM_H_ /** * @file oskar_mem.h * * @brief Structure to wrap a memory pointer either on the CPU or GPU. * * @details * This structure holds a pointer to memory either on the CPU * or GPU, and defines the type of the data to which it points. * * The structure will normally take ownership of the memory, so the * memory will be released when the structure is freed. */ #include <oskar_global.h> #ifdef OSKAR_HAVE_OPENCL #define CL_USE_DEPRECATED_OPENCL_1_2_APIS #ifdef __APPLE__ #include <OpenCL/opencl.h> #else #include <CL/cl.h> #endif #endif /* Public interface. */ #ifdef __cplusplus extern "C" { #endif struct oskar_Mem; #ifndef OSKAR_MEM_TYPEDEF_ #define OSKAR_MEM_TYPEDEF_ typedef struct oskar_Mem oskar_Mem; #endif /* OSKAR_MEM_TYPEDEF_ */ enum OSKAR_MEM_LOCATION { OSKAR_CPU = 0, OSKAR_GPU = 1, /* Bit-field, for future expansion. */ OSKAR_CL = 2 }; #ifdef __cplusplus } #endif #include <binary/oskar_binary_data_types.h> #include <mem/oskar_mem_accessors.h> #include <mem/oskar_mem_add.h> #include <mem/oskar_mem_add_real.h> #include <mem/oskar_mem_append_raw.h> #include <mem/oskar_mem_clear_contents.h> #include <mem/oskar_mem_copy.h> #include <mem/oskar_mem_copy_contents.h> #include <mem/oskar_mem_convert_precision.h> #include <mem/oskar_mem_create.h> #include <mem/oskar_mem_create_alias.h> #include <mem/oskar_mem_create_alias_from_raw.h> #include <mem/oskar_mem_create_copy.h> #include <mem/oskar_mem_data_type_string.h> #include <mem/oskar_mem_different.h> #include <mem/oskar_mem_element_size.h> #include <mem/oskar_mem_evaluate_relative_error.h> #include <mem/oskar_mem_free.h> #include <mem/oskar_mem_get_element.h> #include <mem/oskar_mem_load_ascii.h> #include <mem/oskar_mem_multiply.h> #include <mem/oskar_mem_random_gaussian.h> #include <mem/oskar_mem_random_range.h> #include <mem/oskar_mem_random_uniform.h> #include <mem/oskar_mem_read_binary_raw.h> #include <mem/oskar_mem_read_fits_image_plane.h> #include <mem/oskar_mem_read_healpix_fits.h> #include <mem/oskar_mem_realloc.h> #include <mem/oskar_mem_save_ascii.h> #include <mem/oskar_mem_scale_real.h> #include <mem/oskar_mem_set_alias.h> #include <mem/oskar_mem_set_element.h> #include <mem/oskar_mem_set_value_real.h> #include <mem/oskar_mem_stats.h> #include <mem/oskar_mem_write_fits_cube.h> #include <mem/oskar_mem_write_healpix_fits.h> #endif /* OSKAR_MEM_H_ */
davepallot/OSKAR
oskar/sky/src/oskar_sky_from_fits_file.c
<reponame>davepallot/OSKAR /* * Copyright (c) 2016, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "sky/oskar_sky.h" #include "convert/oskar_convert_brightness_to_jy.h" #include "math/oskar_cmath.h" #include <fitsio.h> #ifdef __cplusplus extern "C" { #endif oskar_Sky* oskar_sky_from_fits_file(int precision, const char* filename, double min_peak_fraction, double min_abs_val, const char* default_map_units, int override_units, double frequency_hz, double spectral_index, int* status) { double image_crval_deg[2], image_crpix[2]; double image_cellsize_deg = 0.0, image_freq_hz = 0.0; double beam_area_pixels = 0.0, pixel_area_sr = 0.0; char *reported_map_units = 0, ordering = 0, coordsys = 0; int naxis = 0, nside = 0; int image_size[2]; oskar_Sky* t = 0; oskar_Mem* data = 0; fitsfile* fptr; /* Determine whether this is a regular FITS image or HEALPix data. */ fits_open_file(&fptr, filename, READONLY, status); if (*status || !fptr) { *status = OSKAR_ERR_FILE_IO; return 0; } fits_get_img_dim(fptr, &naxis, status); fits_close_file(fptr, status); if (naxis == 0) { /* Try to load HEALPix data. */ data = oskar_mem_read_healpix_fits(filename, 0, &nside, &ordering, &coordsys, &reported_map_units, status); pixel_area_sr = (4.0 * M_PI) / oskar_mem_length(data); /* Check HEALPix ordering scheme. */ if (!*status && ordering != 'R') { *status = OSKAR_ERR_FILE_IO; fprintf(stderr, "HEALPix data is not in RING format.\n"); } } else { /* Try to load image pixels. */ data = oskar_mem_read_fits_image_plane(filename, 0, 0, 0, image_size, image_crval_deg, image_crpix, &image_cellsize_deg, 0, &image_freq_hz, &beam_area_pixels, &reported_map_units, status); pixel_area_sr = pow(image_cellsize_deg * M_PI / 180.0, 2.0); } /* Make sure pixels are in Jy. */ if (image_freq_hz == 0.0) image_freq_hz = frequency_hz; oskar_convert_brightness_to_jy(data, beam_area_pixels, pixel_area_sr, image_freq_hz, min_peak_fraction, min_abs_val, reported_map_units, default_map_units, override_units, status); free(reported_map_units); /* Convert the image into a sky model. */ if (naxis == 0) t = oskar_sky_from_healpix_ring(precision, data, image_freq_hz, spectral_index, nside, (coordsys == 'G'), status); else t = oskar_sky_from_image(precision, data, image_size, image_crval_deg, image_crpix, image_cellsize_deg, image_freq_hz, spectral_index, status); /* Free pixel data and return sky model. */ oskar_mem_free(data, status); return t; } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/telescope/station/src/oskar_station_free.c
/* * Copyright (c) 2011-2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "telescope/station/private_station.h" #include "telescope/station/oskar_station.h" #include <stdlib.h> #ifdef __cplusplus extern "C" { #endif void oskar_station_free(oskar_Station* model, int* status) { int i = 0; if (!model) return; /* Free the element data. */ oskar_mem_free(model->element_true_x_enu_metres, status); oskar_mem_free(model->element_true_y_enu_metres, status); oskar_mem_free(model->element_true_z_enu_metres, status); oskar_mem_free(model->element_measured_x_enu_metres, status); oskar_mem_free(model->element_measured_y_enu_metres, status); oskar_mem_free(model->element_measured_z_enu_metres, status); oskar_mem_free(model->element_weight, status); oskar_mem_free(model->element_gain, status); oskar_mem_free(model->element_gain_error, status); oskar_mem_free(model->element_phase_offset_rad, status); oskar_mem_free(model->element_phase_error_rad, status); oskar_mem_free(model->element_x_alpha_cpu, status); oskar_mem_free(model->element_x_beta_cpu, status); oskar_mem_free(model->element_x_gamma_cpu, status); oskar_mem_free(model->element_y_alpha_cpu, status); oskar_mem_free(model->element_y_beta_cpu, status); oskar_mem_free(model->element_y_gamma_cpu, status); oskar_mem_free(model->element_types, status); oskar_mem_free(model->element_types_cpu, status); oskar_mem_free(model->element_mount_types_cpu, status); oskar_mem_free(model->permitted_beam_az_rad, status); oskar_mem_free(model->permitted_beam_el_rad, status); /* Free the noise model. */ oskar_mem_free(model->noise_freq_hz, status); oskar_mem_free(model->noise_rms_jy, status); /* Free the element pattern data if it exists. */ if (oskar_station_has_element(model)) { for (i = 0; i < model->num_element_types; ++i) { oskar_element_free(oskar_station_element(model, i), status); } /* Free the element model handle array. */ free(model->element); } /* Recursively free the child stations. */ if (oskar_station_has_child(model)) { for (i = 0; i < model->num_elements; ++i) { oskar_station_free(oskar_station_child(model, i), status); } /* Free the child station handle array. */ free(model->child); } /* Free the structure itself. */ free(model); } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/imager/src/oskar_imager_finalise.c
/* * Copyright (c) 2016-2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifdef OSKAR_HAVE_CUDA #include <cufft.h> #endif #include "imager/private_imager.h" #include "imager/oskar_imager.h" #include "imager/oskar_grid_correction.h" #include "imager/oskar_grid_functions_pillbox.h" #include "imager/oskar_grid_functions_spheroidal.h" #include "math/oskar_fftpack_cfft.h" #include "math/oskar_fftpack_cfft_f.h" #include "math/oskar_fftphase.h" #include "mem/oskar_mem.h" #include "utility/oskar_device_utils.h" #include "utility/oskar_timer.h" #include <fitsio.h> #include <math.h> #include <stdlib.h> #include <string.h> #ifdef __cplusplus extern "C" { #endif static void write_plane(oskar_Imager* h, oskar_Mem* plane, int c, int p, int* status); void oskar_imager_finalise(oskar_Imager* h, int num_output_images, oskar_Mem** output_images, int num_output_grids, oskar_Mem** output_grids, int* status) { size_t n; int c, p, i, plane_size; if (*status || !h->planes) return; /* Adjust normalisation if required. */ if (h->scale_norm_with_num_input_files) { for (i = 0; i < h->num_planes; ++i) h->plane_norm[i] /= h->num_files; } /* Copy grids to output grid planes if given. */ for (i = 0; (i < h->num_planes) && (i < num_output_grids); ++i) { if (!(output_grids[i])) output_grids[i] = oskar_mem_create(oskar_mem_type(h->planes[i]), OSKAR_CPU, 0, status); oskar_mem_copy(output_grids[i], h->planes[i], status); oskar_mem_scale_real(output_grids[i], 1.0 / h->plane_norm[i], status); } /* Check if images are required. */ if (h->fits_file[0] || output_images) { n = h->image_size * h->image_size; plane_size = oskar_imager_plane_size(h); /* Finalise all the planes. */ for (i = 0; i < h->num_planes; ++i) { oskar_imager_finalise_plane(h, h->planes[i], h->plane_norm[i], status); oskar_imager_trim_image(h, h->planes[i], plane_size, h->image_size, status); } /* Copy images to output image planes if given. */ for (i = 0; (i < h->num_planes) && (i < num_output_images); ++i) { if (!(output_images[i])) output_images[i] = oskar_mem_create(h->imager_prec, OSKAR_CPU, n, status); if (oskar_mem_length(output_images[i]) < n) oskar_mem_realloc(output_images[i], n, status); memcpy(oskar_mem_void(output_images[i]), oskar_mem_void_const(h->planes[i]), n * oskar_mem_element_size(h->imager_prec)); } /* Write to files if required. */ oskar_timer_resume(h->tmr_write); for (c = 0, i = 0; c < h->num_im_channels; ++c) for (p = 0; p < h->num_im_pols; ++p, ++i) write_plane(h, h->planes[i], c, p, status); oskar_timer_pause(h->tmr_write); } /* Record time taken. */ if (h->log) { size_t j, log_size = 0, length = 0; char* log_data; oskar_log_set_value_width(h->log, 25); oskar_log_section(h->log, 'M', "Imager timing"); oskar_log_value(h->log, 'M', 0, "Initialise", "%.3f s", oskar_timer_elapsed(h->tmr_init)); oskar_log_value(h->log, 'M', 0, "Grid update", "%.3f s", oskar_timer_elapsed(h->tmr_grid_update)); oskar_log_value(h->log, 'M', 0, "Grid finalise", "%.3f s", oskar_timer_elapsed(h->tmr_grid_finalise)); oskar_log_value(h->log, 'M', 0, "Read visibility data", "%.3f s", oskar_timer_elapsed(h->tmr_read)); oskar_log_value(h->log, 'M', 0, "Write image data", "%.3f s", oskar_timer_elapsed(h->tmr_write)); oskar_log_section(h->log, 'M', "Imaging complete"); if (h->output_root) { oskar_log_message(h->log, 'M', 0, "Output(s):"); for (i = 0; i < h->num_im_pols; ++i) oskar_log_value(h->log, 'M', 1, "FITS file", "%s", h->output_name[i]); } /* Write log to the output FITS files as HISTORY entries. * Replace newlines with zeros. */ log_data = oskar_log_file_data(h->log, &log_size); for (j = 0; j < log_size; ++j) { if (log_data[j] == '\n') log_data[j] = 0; if (log_data[j] == '\r') log_data[j] = ' '; } for (i = 0; i < h->num_im_pols; ++i) { const char* line = log_data; length = log_size; for (; log_size > 0;) { const char* eol; fits_write_history(h->fits_file[i], line, status); eol = (const char*) memchr(line, '\0', length); if (!eol) break; eol += 1; length -= (eol - line); line = eol; } } free(log_data); } /* Reset imager memory. */ oskar_imager_reset_cache(h, status); } void oskar_imager_finalise_plane(oskar_Imager* h, oskar_Mem* plane, double plane_norm, int* status) { int size; size_t num_cells; if (*status) return; /* Apply normalisation. */ if (plane_norm > 0.0 || plane_norm < 0.0) { oskar_timer_resume(h->tmr_grid_finalise); oskar_mem_scale_real(plane, 1.0 / plane_norm, status); oskar_timer_pause(h->tmr_grid_finalise); } /* If algorithm if DFT, we've finished here. */ if (h->algorithm == OSKAR_ALGORITHM_DFT_2D || h->algorithm == OSKAR_ALGORITHM_DFT_3D) return; /* Check plane is complex type, as plane must be gridded visibilities. */ if (!oskar_mem_is_complex(plane)) { *status = OSKAR_ERR_BAD_DATA_TYPE; return; } /* Check plane size is as expected. */ size = oskar_imager_plane_size(h); num_cells = size * size; if (oskar_mem_length(plane) != num_cells) { *status = OSKAR_ERR_DIMENSION_MISMATCH; return; } /* Perform FFT shift of the input grid. */ oskar_timer_resume(h->tmr_grid_finalise); if (oskar_mem_precision(plane) == OSKAR_DOUBLE) oskar_fftphase_cd(size, size, oskar_mem_double(plane, status)); else oskar_fftphase_cf(size, size, oskar_mem_float(plane, status)); /* Call FFT. */ #ifdef OSKAR_HAVE_CUDA if (h->fft_on_gpu && h->num_gpus > 0) { size_t work_size = 0; oskar_Mem *plane_gpu = 0, *plane_ptr = 0; oskar_device_set(h->gpu_ids[0], status); if (cufftGetSize(h->cufft_plan, &work_size) != CUFFT_SUCCESS) cufftPlan2d(&h->cufft_plan, size, size, (h->imager_prec == OSKAR_DOUBLE) ? CUFFT_Z2Z : CUFFT_C2C); plane_ptr = plane; if (oskar_mem_location(plane) != OSKAR_GPU) { plane_gpu = oskar_mem_create_copy(plane, OSKAR_GPU, status); plane_ptr = plane_gpu; } if (h->imager_prec == OSKAR_DOUBLE) cufftExecZ2Z(h->cufft_plan, oskar_mem_void(plane_ptr), oskar_mem_void(plane_ptr), CUFFT_FORWARD); else cufftExecC2C(h->cufft_plan, oskar_mem_void(plane_ptr), oskar_mem_void(plane_ptr), CUFFT_FORWARD); if (oskar_mem_location(plane) != OSKAR_GPU) oskar_mem_copy(plane, plane_ptr, status); oskar_mem_free(plane_gpu, status); } else #endif { if (!h->fftpack_work) h->fftpack_work = oskar_mem_create(h->imager_prec, OSKAR_CPU, 2 * num_cells, status); if (!h->fftpack_wsave) { int len = 4 * size + 2 * (int)(log((double)size) / log(2.0)) + 8; h->fftpack_wsave = oskar_mem_create(h->imager_prec, OSKAR_CPU, len, status); if (h->imager_prec == OSKAR_DOUBLE) oskar_fftpack_cfft2i(size, size, oskar_mem_double(h->fftpack_wsave, status)); else oskar_fftpack_cfft2i_f(size, size, oskar_mem_float(h->fftpack_wsave, status)); } if (h->imager_prec == OSKAR_DOUBLE) oskar_fftpack_cfft2f(size, size, size, oskar_mem_double(plane, status), oskar_mem_double(h->fftpack_wsave, status), oskar_mem_double(h->fftpack_work, status)); else oskar_fftpack_cfft2f_f(size, size, size, oskar_mem_float(plane, status), oskar_mem_float(h->fftpack_wsave, status), oskar_mem_float(h->fftpack_work, status)); oskar_mem_scale_real(plane, (double)num_cells, status); } /* Generate grid correction function if required. */ if (!h->corr_func) { h->corr_func = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, size, status); if (h->algorithm != OSKAR_ALGORITHM_FFT) oskar_grid_correction_function_spheroidal(size, h->oversample, oskar_mem_double(h->corr_func, status)); else { if (h->kernel_type == 'S') oskar_grid_correction_function_spheroidal(size, 0, oskar_mem_double(h->corr_func, status)); else if (h->kernel_type == 'P') oskar_grid_correction_function_pillbox(size, oskar_mem_double(h->corr_func, status)); } } /* FFT shift again, and apply grid correction. */ if (oskar_mem_precision(plane) == OSKAR_DOUBLE) { oskar_fftphase_cd(size, size, oskar_mem_double(plane, status)); oskar_grid_correction_d(size, oskar_mem_double(h->corr_func, status), oskar_mem_double(plane, status)); } else { oskar_fftphase_cf(size, size, oskar_mem_float(plane, status)); oskar_grid_correction_f(size, oskar_mem_double(h->corr_func, status), oskar_mem_float(plane, status)); } oskar_timer_pause(h->tmr_grid_finalise); } void oskar_imager_trim_image(oskar_Imager* h, oskar_Mem* plane, int plane_size, int image_size, int* status) { int size_diff; if (*status) return; /* Get the real part only, if the plane is complex. */ oskar_timer_resume(h->tmr_grid_finalise); if (oskar_mem_is_complex(plane)) { size_t i, num_cells; num_cells = plane_size * plane_size; if (oskar_mem_precision(plane) == OSKAR_DOUBLE) { double *t = oskar_mem_double(plane, status); for (i = 0; i < num_cells; ++i) t[i] = t[2 * i]; } else { float *t = oskar_mem_float(plane, status); for (i = 0; i < num_cells; ++i) t[i] = t[2 * i]; } } /* Trim to required image size. */ size_diff = plane_size - image_size; if (size_diff > 0) { char *ptr; size_t in = 0, out = 0, copy_len = 0, element_size = 0; int i; ptr = oskar_mem_char(plane); element_size = oskar_mem_element_size(oskar_mem_precision(plane)); copy_len = element_size * image_size; in = element_size * (size_diff / 2) * (plane_size + 1); for (i = 0; i < image_size; ++i) { /* Use memmove() instead of memcpy() to allow for overlap. */ memmove(ptr + out, ptr + in, copy_len); in += plane_size * element_size; out += copy_len; } } oskar_timer_pause(h->tmr_grid_finalise); } void write_plane(oskar_Imager* h, oskar_Mem* plane, int c, int p, int* status) { int datatype, num_pixels; long firstpix[3]; if (*status) return; if (!h->fits_file[p]) return; datatype = (oskar_mem_is_double(plane) ? TDOUBLE : TFLOAT); firstpix[0] = 1; firstpix[1] = 1; firstpix[2] = 1 + c; num_pixels = h->image_size * h->image_size; fits_write_pix(h->fits_file[p], datatype, firstpix, num_pixels, oskar_mem_void(plane), status); } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/vis/private_vis.h
/* * Copyright (c) 2011-2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_PRIVATE_VIS_H_ #define OSKAR_PRIVATE_VIS_H_ #include <mem/oskar_mem.h> /* * Deprecated structure to hold visibility data. * * Visibility data consists of a set of baseline (u,v,w) coordinates and * one or more complex visibility amplitudes. * * Baseline coordinates are stored as three separate arrays of length corresponding * to the number of visibility samples in the data set. Coordinates are real * valued and can be either double or single precision. Coordinates are ordered * by channel ID, time and baseline where channel is the slowest varying dimension * and baseline is the fastest varying. * * Axis order: channel(slowest) -> time -> baseline (fastest) * * Visibility amplitudes are sorted as an array of complex scalar or 2-by-2 matrix * elements which can be either double or single precision according to the * amplitude type ID. The dimension ordering is the same as that used for * the baseline coordinate arrays. The use of scalar or matrix elements in the * array determines the number of polarisations stored in the data. Scalar elements * represent a single polarisation axis whereas matrix elements store 4 polarisations * per data sample. */ struct oskar_Vis { oskar_Mem* settings_path; /* Path to settings file. */ oskar_Mem* telescope_path; /* Path to telescope model. */ oskar_Mem* settings; /* Settings file contents. */ int num_channels; /* Number of frequency channels. */ int num_times; /* Number of time samples. */ int num_stations; /* Number of interferometer stations. */ int num_baselines; /* Number of interferometer baselines. */ double freq_start_hz; /* Start frequency, in Hz. */ double freq_inc_hz; /* Frequency increment, in Hz. */ double channel_bandwidth_hz; /* Frequency channel bandwidth, in Hz */ double time_start_mjd_utc; /* Start time in MJD, UTC. */ double time_inc_sec; /* Time increment, in seconds. */ double time_average_sec; /* Time average smearing duration, in seconds. */ double phase_centre_ra_deg; /* Pointing phase centre RA, in degrees. */ double phase_centre_dec_deg; /* Pointing phase centre Dec, in degrees. */ double telescope_lon_deg; /* Reference longitude of the telescope, in degrees */ double telescope_lat_deg; /* Reference latitude of the telescope, in degrees */ double telescope_alt_metres; /* Reference altitude of the telescope, in metres. */ oskar_Mem* station_x_offset_ecef_metres; /* Station x-coordinate, in metres (offset ECEF). */ oskar_Mem* station_y_offset_ecef_metres; /* Station y-coordinate, in metres (offset ECEF). */ oskar_Mem* station_z_offset_ecef_metres; /* Station z-coordinate, in metres (offset ECEF). */ oskar_Mem* baseline_uu_metres; /* Baseline coordinates, in metres. */ oskar_Mem* baseline_vv_metres; /* Baseline coordinates, in metres. */ oskar_Mem* baseline_ww_metres; /* Baseline coordinates, in metres. */ oskar_Mem* amplitude; /* Complex visibility amplitude. */ }; #ifndef OSKAR_VIS_TYPEDEF_ #define OSKAR_VIS_TYPEDEF_ typedef struct oskar_Vis oskar_Vis; #endif /* OSKAR_VIS_TYPEDEF_ */ #endif /* OSKAR_PRIVATE_VIS_H_ */
davepallot/OSKAR
oskar/convert/private_convert_ecef_to_station_uvw_inline.h
/* * Copyright (c) 2014, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_CONVERT_ECEF_TO_STATION_UVW_INLINE_H_ #define OSKAR_CONVERT_ECEF_TO_STATION_UVW_INLINE_H_ #include <oskar_global.h> #ifdef __cplusplus extern "C" { #endif /* Single precision. */ OSKAR_INLINE void oskar_convert_ecef_to_station_uvw_inline_f(const float x, const float y, const float z, const float sin_ha0, const float cos_ha0, const float sin_dec0, const float cos_dec0, float* u, float* v, float* w) { float v_, w_, t; /* This is just the standard textbook rotation matrix. */ t = x * cos_ha0; t -= y * sin_ha0; v_ = z * cos_dec0; v_ -= sin_dec0 * t; w_ = cos_dec0 * t; w_ += z * sin_dec0; t = x * sin_ha0; t += y * cos_ha0; *u = t; *v = v_; *w = w_; } /* Double precision. */ OSKAR_INLINE void oskar_convert_ecef_to_station_uvw_inline_d(const double x, const double y, const double z, const double sin_ha0, const double cos_ha0, const double sin_dec0, const double cos_dec0, double* u, double* v, double* w) { double v_, w_, t; /* This is just the standard textbook rotation matrix. */ t = x * cos_ha0; t -= y * sin_ha0; v_ = z * cos_dec0; v_ -= sin_dec0 * t; w_ = cos_dec0 * t; w_ += z * sin_dec0; t = x * sin_ha0; t += y * cos_ha0; *u = t; *v = v_; *w = w_; } #ifdef __cplusplus } #endif #endif /* OSKAR_CONVERT_ECEF_TO_STATION_UVW_INLINE_H_ */
davepallot/OSKAR
oskar/interferometer/src/oskar_jones_accessors.c
/* * Copyright (c) 2013, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "interferometer/private_jones.h" #include "interferometer/oskar_jones_accessors.h" #include "mem/oskar_mem.h" #ifdef __cplusplus extern "C" { #endif int oskar_jones_num_sources(const oskar_Jones* jones) { return jones->num_sources; } int oskar_jones_num_stations(const oskar_Jones* jones) { return jones->num_stations; } int oskar_jones_type(const oskar_Jones* jones) { return oskar_mem_type(jones->data); } int oskar_jones_mem_location(const oskar_Jones* jones) { return oskar_mem_location(jones->data); } oskar_Mem* oskar_jones_mem(oskar_Jones* jones) { return jones->data; } const oskar_Mem* oskar_jones_mem_const(const oskar_Jones* jones) { return jones->data; } /* Single precision. */ float2* oskar_jones_float2(oskar_Jones* jones, int* status) { return oskar_mem_float2(jones->data, status); } const float2* oskar_jones_float2_const(const oskar_Jones* jones, int* status) { return oskar_mem_float2_const(jones->data, status); } float4c* oskar_jones_float4c(oskar_Jones* jones, int* status) { return oskar_mem_float4c(jones->data, status); } const float4c* oskar_jones_float4c_const(const oskar_Jones* jones, int* status) { return oskar_mem_float4c_const(jones->data, status); } /* Double precision. */ double2* oskar_jones_double2(oskar_Jones* jones, int* status) { return oskar_mem_double2(jones->data, status); } const double2* oskar_jones_double2_const(const oskar_Jones* jones, int* status) { return oskar_mem_double2_const(jones->data, status); } double4c* oskar_jones_double4c(oskar_Jones* jones, int* status) { return oskar_mem_double4c(jones->data, status); } const double4c* oskar_jones_double4c_const(const oskar_Jones* jones, int* status) { return oskar_mem_double4c_const(jones->data, status); } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/telescope/station/private_station_work.h
/* * Copyright (c) 2012-2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_PRIVATE_STATION_WORK_H_ #define OSKAR_PRIVATE_STATION_WORK_H_ #include <mem/oskar_mem.h> struct oskar_StationWork { oskar_Mem* horizon_mask; /* Integer. */ oskar_Mem* source_indices; /* Integer. */ oskar_Mem* enu_direction_x; /* Real scalar. ENU direction cosine. */ oskar_Mem* enu_direction_y; /* Real scalar. ENU direction cosine. */ oskar_Mem* enu_direction_z; /* Real scalar. ENU direction cosine. */ oskar_Mem* theta_modified; /* Real scalar. */ oskar_Mem* phi_modified; /* Real scalar. */ oskar_Mem* weights; /* Complex scalar. */ oskar_Mem* weights_error; /* Complex scalar. */ oskar_Mem* array_pattern; /* Complex scalar. */ oskar_Mem* normalised_beam; /* For beam normalisation. */ int num_depths; oskar_Mem** beam; /* For hierarchical stations. */ }; #ifndef OSKAR_STATION_WORK_TYPEDEF_ #define OSKAR_STATION_WORK_TYPEDEF_ typedef struct oskar_StationWork oskar_StationWork; #endif /* OSKAR_STATION_WORK_TYPEDEF_ */ #endif /* OSKAR_PRIVATE_STATION_WORK_H_ */
davepallot/OSKAR
oskar/mem/src/oskar_mem_random_uniform.c
/* * Copyright (c) 2015-2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "mem/oskar_mem.h" #include "mem/oskar_mem_random_uniform_cuda.h" #include "math/private_random_helpers.h" #include "utility/oskar_cl_utils.h" #include "utility/oskar_device_utils.h" #ifdef __cplusplus extern "C" { #endif void oskar_mem_random_uniform_f( const int num_elements, float* data, const unsigned int seed, const unsigned int counter1, const unsigned int counter2, const unsigned int counter3) { int i, i4, n1; n1 = num_elements / 4; #pragma omp parallel for private(i, i4) for (i = 0; i < n1; ++i) { OSKAR_R123_GENERATE_4(seed, i, counter1, counter2, counter3) /* Convert to uniform float. */ i4 = i * 4; data[i4] = oskar_int_to_range_0_to_1_f(u.i[0]); data[i4 + 1] = oskar_int_to_range_0_to_1_f(u.i[1]); data[i4 + 2] = oskar_int_to_range_0_to_1_f(u.i[2]); data[i4 + 3] = oskar_int_to_range_0_to_1_f(u.i[3]); } if (num_elements % 4) { OSKAR_R123_GENERATE_4(seed, n1, counter1, counter2, counter3) /* Convert to uniform float. */ i4 = n1 * 4; data[i4] = oskar_int_to_range_0_to_1_f(u.i[0]); if (i4 + 1 < num_elements) data[i4 + 1] = oskar_int_to_range_0_to_1_f(u.i[1]); if (i4 + 2 < num_elements) data[i4 + 2] = oskar_int_to_range_0_to_1_f(u.i[2]); if (i4 + 3 < num_elements) data[i4 + 3] = oskar_int_to_range_0_to_1_f(u.i[3]); } } void oskar_mem_random_uniform_d( const int num_elements, double* data, const unsigned int seed, const unsigned int counter1, const unsigned int counter2, const unsigned int counter3) { int i, i4, n1; n1 = num_elements / 4; #pragma omp parallel for private(i, i4) for (i = 0; i < n1; ++i) { OSKAR_R123_GENERATE_4(seed, i, counter1, counter2, counter3) /* Convert to uniform float. */ i4 = i * 4; data[i4] = oskar_int_to_range_0_to_1_d(u.i[0]); data[i4 + 1] = oskar_int_to_range_0_to_1_d(u.i[1]); data[i4 + 2] = oskar_int_to_range_0_to_1_d(u.i[2]); data[i4 + 3] = oskar_int_to_range_0_to_1_d(u.i[3]); } if (num_elements % 4) { OSKAR_R123_GENERATE_4(seed, n1, counter1, counter2, counter3) /* Convert to uniform float. */ i4 = n1 * 4; data[i4] = oskar_int_to_range_0_to_1_d(u.i[0]); if (i4 + 1 < num_elements) data[i4 + 1] = oskar_int_to_range_0_to_1_d(u.i[1]); if (i4 + 2 < num_elements) data[i4 + 2] = oskar_int_to_range_0_to_1_d(u.i[2]); if (i4 + 3 < num_elements) data[i4 + 3] = oskar_int_to_range_0_to_1_d(u.i[3]); } } void oskar_mem_random_uniform(oskar_Mem* data, unsigned int seed, unsigned int counter1, unsigned int counter2, unsigned int counter3, int* status) { int type, location; size_t num_elements; #ifdef OSKAR_HAVE_OPENCL cl_kernel k = 0; #endif /* Check if safe to proceed. */ if (*status) return; type = oskar_mem_precision(data); location = oskar_mem_location(data); num_elements = oskar_mem_length(data); if (oskar_mem_is_complex(data)) num_elements *= 2; if (oskar_mem_is_matrix(data)) num_elements *= 4; if (location == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA if (type == OSKAR_SINGLE) oskar_mem_random_uniform_cuda_f((int)num_elements, oskar_mem_float(data, status), seed, counter1, counter2, counter3); else if (type == OSKAR_DOUBLE) oskar_mem_random_uniform_cuda_d((int)num_elements, oskar_mem_double(data, status), seed, counter1, counter2, counter3); oskar_device_check_error(status); #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } else if (location == OSKAR_CPU) { if (type == OSKAR_SINGLE) oskar_mem_random_uniform_f((int)num_elements, oskar_mem_float(data, status), seed, counter1, counter2, counter3); else if (type == OSKAR_DOUBLE) oskar_mem_random_uniform_d((int)num_elements, oskar_mem_double(data, status), seed, counter1, counter2, counter3); } else if (location & OSKAR_CL) { #ifdef OSKAR_HAVE_OPENCL if (type == OSKAR_SINGLE) k = oskar_cl_kernel("mem_random_uniform_float"); else if (type == OSKAR_DOUBLE) k = oskar_cl_kernel("mem_random_uniform_double"); if (k) { cl_device_type dev_type; cl_event event; cl_int error, gpu; cl_uint n, s, c1, c2, c3; size_t global_size, local_size; /* Set kernel arguments. */ clGetDeviceInfo(oskar_cl_device_id(), CL_DEVICE_TYPE, sizeof(cl_device_type), &dev_type, NULL); gpu = dev_type & CL_DEVICE_TYPE_GPU; n = (cl_uint) num_elements; s = (cl_uint) seed; c1 = (cl_uint) counter1; c2 = (cl_uint) counter2; c3 = (cl_uint) counter3; error = clSetKernelArg(k, 0, sizeof(cl_uint), &n); error |= clSetKernelArg(k, 1, sizeof(cl_mem), oskar_mem_cl_buffer(data, status)); error |= clSetKernelArg(k, 2, sizeof(cl_uint), &s); error |= clSetKernelArg(k, 3, sizeof(cl_uint), &c1); error |= clSetKernelArg(k, 4, sizeof(cl_uint), &c2); error |= clSetKernelArg(k, 5, sizeof(cl_uint), &c3); if (*status) return; if (error != CL_SUCCESS) { *status = OSKAR_ERR_INVALID_ARGUMENT; return; } /* Launch kernel on current command queue. */ local_size = gpu ? 256 : 128; global_size = ((((num_elements + 3) / 4) + local_size - 1) / local_size) * local_size; error = clEnqueueNDRangeKernel(oskar_cl_command_queue(), k, 1, NULL, &global_size, &local_size, 0, NULL, &event); if (error != CL_SUCCESS) { *status = OSKAR_ERR_KERNEL_LAUNCH_FAILURE; return; } } else { *status = OSKAR_ERR_FUNCTION_NOT_AVAILABLE; } #else *status = OSKAR_ERR_OPENCL_NOT_AVAILABLE; #endif } else *status = OSKAR_ERR_BAD_LOCATION; } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/telescope/station/element/oskar_evaluate_dipole_pattern_inline.h
<reponame>davepallot/OSKAR /* * Copyright (c) 2014, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include <oskar_global.h> #include <math.h> #ifdef __cplusplus extern "C" { #endif /* * Algorithm is as follows. * * Start with original expression for dipole pattern, with theta_prime the * polar angle from the dipole axis: * * E_theta_prime = (cos(kL * cos(theta_prime)) - cos_kL) / sin(theta_prime) * (E_phi_prime = 0) * * Substitute theta_prime = acos(cos_phi * sin_theta): * * E_theta_prime = (cos(kL * cos_phi * sin_theta) - cos_kL) / * sqrt(1 - cos_phi*cos_phi * sin_theta*sin_theta) * * Then rotate E_theta_prime vector to express as components along * the required E_theta and E_phi directions. This is performed using the * difference of bearing angles between the required point (start) and the * end of the dipole axis (finish): * * beta = atan2(-sin_phi, -cos_theta * cos_phi) * E_theta = cos(beta) * E_theta_prime; E_phi = -sin(beta) * E_theta_prime; * * (The same result can be achieved by projecting E_theta_prime along both the * E_theta and E_phi directions, but this is more efficient.) * * Simplifying gives: * * E_theta = -cos_phi * cos_theta * (cos(kL * cos_phi * sin_theta) - cos_kL) * / (1 + cos_phi*cos_phi * (cos_theta*cos_theta - 1)) * E_phi = sin_phi * (cos(kL * cos_phi * sin_theta) - cos_kL) * / (1 + cos_phi*cos_phi * (cos_theta*cos_theta - 1)) */ /** * @brief Evaluates ideal dipole pattern at a single point (single precision). * * @details * This inline function evaluates the response of an ideal dipole at a single * point. The dipole is oriented along the x-axis. The angle phi is the * azimuthal angle from the x-axis towards the y-axis, and the angle theta is * the polar angle from the z-axis to the xy-plane. * * \f{eqnarray*){ * E_{\theta} &=& * \frac{-\cos\phi \cos\theta (\cos(k L \cos\phi \sin\theta) - \cos(k L))} * {1 + \cos^2\phi (\cos^2\theta - 1)} * E_{\phi} &=& * \frac{\sin\phi (\cos(k L \cos\phi \sin\theta) - \cos(k L))} * {1 + \cos^2\phi (\cos^2\theta - 1)} * \f} * * @param[in] theta Polar angle in radians. * @param[in] phi Azimuthal angle in radians. * @param[in] kL Wavenumber multiplied by dipole length in metres. * @param[in] cos_kL Cosine of \p kL. * @param[out] E_theta Complex dipole response in theta direction. * @param[out] E_phi Complex dipole response in phi direction. */ OSKAR_INLINE void oskar_evaluate_dipole_pattern_inline_f(const float theta, const float phi, const float kL, const float cos_kL, float2* E_theta, float2* E_phi) { float sin_phi, cos_phi, sin_theta, cos_theta, t, denom; #ifdef __CUDACC__ sincosf(theta, &sin_theta, &cos_theta); sincosf(phi, &sin_phi, &cos_phi); #else sin_theta = sinf(theta); cos_theta = cosf(theta); sin_phi = sinf(phi); cos_phi = cosf(phi); #endif denom = 1.0f + cos_phi*cos_phi * (cos_theta*cos_theta - 1.0f); if (denom == 0.0f) { /* Early return if point is precisely at either end of the dipole. */ E_theta->x = 0.0f; E_theta->y = 0.0f; E_phi->x = 0.0f; E_phi->y = 0.0f; return; } t = (cosf(kL * cos_phi * sin_theta) - cos_kL) / denom; /* Store real and imaginary components of E_theta, E_phi vectors. */ E_theta->x = -cos_phi * cos_theta * t; E_theta->y = 0.0f; E_phi->x = sin_phi * t; E_phi->y = 0.0f; } /** * @brief Evaluates ideal dipole pattern at a single point (double precision). * * @details * This inline function evaluates the response of an ideal dipole at a single * point. The dipole is oriented along the x-axis. The angle phi is the * azimuthal angle from the x-axis towards the y-axis, and the angle theta is * the polar angle from the z-axis to the xy-plane. * * \f{eqnarray*){ * E_{\theta} &=& * \frac{-\cos\phi \cos\theta (\cos(k L \cos\phi \sin\theta) - \cos(k L))} * {1 + \cos^2\phi (\cos^2\theta - 1)} * E_{\phi} &=& * \frac{\sin\phi (\cos(k L \cos\phi \sin\theta) - \cos(k L))} * {1 + \cos^2\phi (\cos^2\theta - 1)} * \f} * * @param[in] theta Polar angle in radians. * @param[in] phi Azimuthal angle in radians. * @param[in] kL Wavenumber multiplied by dipole length in metres. * @param[in] cos_kL Cosine of \p kL. * @param[out] E_theta Complex dipole response in theta direction. * @param[out] E_phi Complex dipole response in phi direction. */ OSKAR_INLINE void oskar_evaluate_dipole_pattern_inline_d(const double theta, const double phi, const double kL, const double cos_kL, double2* E_theta, double2* E_phi) { double sin_phi, cos_phi, sin_theta, cos_theta, t, denom; #ifdef __CUDACC__ sincos(theta, &sin_theta, &cos_theta); sincos(phi, &sin_phi, &cos_phi); #else sin_theta = sin(theta); cos_theta = cos(theta); sin_phi = sin(phi); cos_phi = cos(phi); #endif denom = 1.0 + cos_phi*cos_phi * (cos_theta*cos_theta - 1.0); if (denom == 0.0) { /* Early return if point is precisely at either end of the dipole. */ E_theta->x = 0.0; E_theta->y = 0.0; E_phi->x = 0.0; E_phi->y = 0.0; return; } t = (cos(kL * cos_phi * sin_theta) - cos_kL) / denom; /* Store real and imaginary components of E_theta, E_phi vectors. */ E_theta->x = -cos_phi * cos_theta * t; E_theta->y = 0.0; E_phi->x = sin_phi * t; E_phi->y = 0.0; } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/vis/src/oskar_vis_header_create_copy.c
/* * Copyright (c) 2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "vis/private_vis_header.h" #include "vis/oskar_vis_header.h" #ifdef __cplusplus extern "C" { #endif oskar_VisHeader* oskar_vis_header_create_copy(const oskar_VisHeader* other, int* status) { oskar_VisHeader* hdr = 0; /* Check if safe to proceed. */ if (*status) return 0; /* Create a new header. */ hdr = oskar_vis_header_create(other->amp_type, other->coord_precision, other->max_times_per_block, other->num_times_total, other->max_channels_per_block, other->num_channels_total, other->num_stations, other->write_autocorr, other->write_crosscorr, status); /* Copy meta-data. */ hdr->pol_type = other->pol_type; hdr->freq_start_hz = other->freq_start_hz; hdr->freq_inc_hz = other->freq_inc_hz; hdr->channel_bandwidth_hz = other->channel_bandwidth_hz; hdr->time_start_mjd_utc = other->time_start_mjd_utc; hdr->time_inc_sec = other->time_inc_sec; hdr->time_average_sec = other->time_average_sec; hdr->phase_centre_type = other->phase_centre_type; hdr->phase_centre_deg[0] = other->phase_centre_deg[0]; hdr->phase_centre_deg[1] = other->phase_centre_deg[1]; hdr->telescope_centre_lon_deg = other->telescope_centre_lon_deg; hdr->telescope_centre_lat_deg = other->telescope_centre_lat_deg; hdr->telescope_centre_alt_m = other->telescope_centre_alt_m; /* Copy memory. */ oskar_mem_copy(hdr->telescope_path, other->telescope_path, status); oskar_mem_copy(hdr->settings, other->settings, status); oskar_mem_copy(hdr->station_x_offset_ecef_metres, other->station_x_offset_ecef_metres, status); oskar_mem_copy(hdr->station_y_offset_ecef_metres, other->station_y_offset_ecef_metres, status); oskar_mem_copy(hdr->station_z_offset_ecef_metres, other->station_z_offset_ecef_metres, status); /* Return handle to structure. */ return hdr; } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/sky/oskar_sky_scale_flux_with_frequency_cuda.h
<filename>oskar/sky/oskar_sky_scale_flux_with_frequency_cuda.h /* * Copyright (c) 2011-2014, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_SKY_SCALE_FLUX_WITH_FREQUENCY_CUDA_H_ #define OSKAR_SKY_SCALE_FLUX_WITH_FREQUENCY_CUDA_H_ /** * @file oskar_sky_scale_flux_with_frequency_cuda.h */ #include <oskar_global.h> #ifdef __cplusplus extern "C" { #endif /** * @brief * CUDA function to scale source fluxes by spectral index values * (single precision). * * @details * This function evaluates all fluxes (all Stokes parameters) at the specified * frequency using the spectral index and rotation measure of each source. * The reference frequency of each source is also updated to the specified * frequency. * * Frequency scaling is performed using the expression: * * \f[ * F = F * (\nu / \nu_0)^\alpha * \f] * * where \f$F\f$ is the flux, \f$\nu\f$ is the new frequency, \f$\nu_0\f$ is * the reference frequency, and \f$\alpha\f$ is the spectral index value. * * Note that all pointers are device pointers, and must not be dereferenced * in host code. * * @param[in] num_sources The number of sources in the input arrays. * @param[in] frequency The frequency at which to evaluate fluxes, in Hz. * @param[in,out] d_I Source Stokes I values. * @param[in,out] d_Q Source Stokes Q values. * @param[in,out] d_U Source Stokes U values. * @param[in,out] d_V Source Stokes V values. * @param[in,out] d_ref_freq Source reference frequency values, in Hz. * @param[in] d_sp_index Source spectral index values. * @param[in] d_rm Source rotation measure values, in rad/m^2. */ OSKAR_EXPORT void oskar_sky_scale_flux_with_frequency_cuda_f(int num_sources, float frequency, float* d_I, float* d_Q, float* d_U, float* d_V, float* d_ref_freq, const float* d_sp_index, const float* d_rm); /** * @brief * CUDA function to scale source fluxes by spectral index values * (double precision). * * @details * This function evaluates all fluxes (all Stokes parameters) at the specified * frequency using the spectral index and rotation measure of each source. * The reference frequency of each source is also updated to the specified * frequency. * * Frequency scaling is performed using the expression: * * \f[ * F = F * (\nu / \nu_0)^\alpha * \f] * * where \f$F\f$ is the flux, \f$\nu\f$ is the new frequency, \f$\nu_0\f$ is * the reference frequency, and \f$\alpha\f$ is the spectral index value. * * Note that all pointers are device pointers, and must not be dereferenced * in host code. * * @param[in] num_sources The number of sources in the input arrays. * @param[in] frequency The frequency at which to evaluate fluxes, in Hz. * @param[in,out] d_I Source Stokes I values. * @param[in,out] d_Q Source Stokes Q values. * @param[in,out] d_U Source Stokes U values. * @param[in,out] d_V Source Stokes V values. * @param[in,out] d_ref_freq Source reference frequency values, in Hz. * @param[in] d_sp_index Source spectral index values. * @param[in] d_rm Source rotation measure values, in rad/m^2. */ OSKAR_EXPORT void oskar_sky_scale_flux_with_frequency_cuda_d(int num_sources, double frequency, double* d_I, double* d_Q, double* d_U, double* d_V, double* d_ref_freq, const double* d_sp_index, const double* d_rm); #ifdef __cplusplus } #endif #endif /* OSKAR_SKY_SCALE_FLUX_WITH_FREQUENCY_CUDA_H_ */
davepallot/OSKAR
oskar/telescope/station/element/src/oskar_element_write.c
/* * Copyright (c) 2014-2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "telescope/station/element/private_element.h" #include "telescope/station/element/oskar_element.h" #include "math/oskar_find_closest_match.h" #include "mem/oskar_binary_write_mem.h" #include "binary/oskar_binary.h" #ifdef __cplusplus extern "C" { #endif static void write_splines(oskar_Binary* h, const oskar_Splines* splines, int index, int* status); void oskar_element_write(const oskar_Element* data, oskar_Log* log, const char* filename, int port, double freq_hz, int* status) { const oskar_Splines *h_re = 0, *h_im = 0, *v_re = 0, *v_im = 0; const oskar_Splines *scalar_re = 0, *scalar_im = 0; oskar_Binary* h = 0; int freq_id; char* log_data = 0; size_t log_size = 0; /* Check if safe to proceed. */ if (*status) return; /* Get the frequency ID. */ freq_id = oskar_find_closest_match_d(freq_hz, oskar_element_num_freq(data), oskar_element_freqs_hz_const(data)); /* Get pointers to surface data based on port number and frequency index. */ if (port == 0) { scalar_re = oskar_element_scalar_re_const(data, freq_id); scalar_im = oskar_element_scalar_im_const(data, freq_id); } else if (port == 1) { h_re = oskar_element_x_h_re_const(data, freq_id); h_im = oskar_element_x_h_im_const(data, freq_id); v_re = oskar_element_x_v_re_const(data, freq_id); v_im = oskar_element_x_v_im_const(data, freq_id); } else if (port == 2) { h_re = oskar_element_y_h_re_const(data, freq_id); h_im = oskar_element_y_h_im_const(data, freq_id); v_re = oskar_element_y_v_re_const(data, freq_id); v_im = oskar_element_y_v_im_const(data, freq_id); } else { *status = OSKAR_ERR_INVALID_ARGUMENT; return; } /* Dump data to a binary file. */ h = oskar_binary_create(filename, 'w', status); if (*status) { oskar_binary_free(h); return; } /* If log exists, then write it out. */ log_data = oskar_log_file_data(log, &log_size); if (log_data) { oskar_binary_write(h, OSKAR_CHAR, OSKAR_TAG_GROUP_RUN, OSKAR_TAG_RUN_LOG, 0, log_size, log_data, status); free(log_data); } if (port == 0) { /* Write the surface type (scalar). */ oskar_binary_write_int(h, OSKAR_TAG_GROUP_ELEMENT_DATA, OSKAR_ELEMENT_TAG_SURFACE_TYPE, 0, OSKAR_ELEMENT_SURFACE_TYPE_SCALAR, status); /* Write data for [real], [imag] surfaces. */ write_splines(h, scalar_re, 0, status); write_splines(h, scalar_im, 1, status); } else { /* Write the surface type (Ludwig-3). */ oskar_binary_write_int(h, OSKAR_TAG_GROUP_ELEMENT_DATA, OSKAR_ELEMENT_TAG_SURFACE_TYPE, 0, OSKAR_ELEMENT_SURFACE_TYPE_LUDWIG_3, status); /* Write data for [h_re], [h_im], [v_re], [v_im] surfaces. */ write_splines(h, h_re, 0, status); write_splines(h, h_im, 1, status); write_splines(h, v_re, 2, status); write_splines(h, v_im, 3, status); } /* Release the handle. */ oskar_binary_free(h); } static void write_splines(oskar_Binary* h, const oskar_Splines* splines, int index, int* status) { const oskar_Mem *knots_x, *knots_y, *coeff; oskar_Mem* temp; unsigned char group = (unsigned char) OSKAR_TAG_GROUP_SPLINE_DATA; if (!splines) { *status = OSKAR_ERR_MEMORY_NOT_ALLOCATED; return; } if (*status) return; knots_x = oskar_splines_knots_x_theta_const(splines); knots_y = oskar_splines_knots_y_phi_const(splines); coeff = oskar_splines_coeff_const(splines); oskar_binary_write_int(h, group, OSKAR_SPLINES_TAG_NUM_KNOTS_X_THETA, index, oskar_splines_num_knots_x_theta(splines), status); oskar_binary_write_int(h, group, OSKAR_SPLINES_TAG_NUM_KNOTS_Y_PHI, index, oskar_splines_num_knots_y_phi(splines), status); /* Write data in double precision. */ temp = oskar_mem_convert_precision(knots_x, OSKAR_DOUBLE, status); oskar_binary_write_mem(h, temp, group, OSKAR_SPLINES_TAG_KNOTS_X_THETA, index, 0, status); oskar_mem_free(temp, status); temp = oskar_mem_convert_precision(knots_y, OSKAR_DOUBLE, status); oskar_binary_write_mem(h, temp, group, OSKAR_SPLINES_TAG_KNOTS_Y_PHI, index, 0, status); oskar_mem_free(temp, status); temp = oskar_mem_convert_precision(coeff, OSKAR_DOUBLE, status); oskar_binary_write_mem(h, temp, group, OSKAR_SPLINES_TAG_COEFF, index, 0, status); oskar_mem_free(temp, status); /* Write data in single precision. */ temp = oskar_mem_convert_precision(knots_x, OSKAR_SINGLE, status); oskar_binary_write_mem(h, temp, group, OSKAR_SPLINES_TAG_KNOTS_X_THETA, index, 0, status); oskar_mem_free(temp, status); temp = oskar_mem_convert_precision(knots_y, OSKAR_SINGLE, status); oskar_binary_write_mem(h, temp, group, OSKAR_SPLINES_TAG_KNOTS_Y_PHI, index, 0, status); oskar_mem_free(temp, status); temp = oskar_mem_convert_precision(coeff, OSKAR_SINGLE, status); oskar_binary_write_mem(h, temp, group, OSKAR_SPLINES_TAG_COEFF, index, 0, status); oskar_mem_free(temp, status); oskar_binary_write_double(h, group, OSKAR_SPLINES_TAG_SMOOTHING_FACTOR, index, oskar_splines_smoothing_factor(splines), status); } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/imager/src/private_imager_init_dft.c
<reponame>davepallot/OSKAR /* * Copyright (c) 2016-2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "imager/private_imager.h" #include "imager/private_imager_init_dft.h" #include "imager/oskar_imager_accessors.h" #include "math/oskar_evaluate_image_lmn_grid.h" #include "math/oskar_cmath.h" #include "utility/oskar_device_utils.h" #ifdef __cplusplus extern "C" { #endif void oskar_imager_init_dft(oskar_Imager* h, int* status) { int i, dev_loc, prec; size_t num_pixels; if (*status) return; /* Calculate pixel coordinate grid required for the DFT imager. */ num_pixels = h->image_size * h->image_size; prec = h->imager_prec; oskar_mem_free(h->l, status); oskar_mem_free(h->m, status); oskar_mem_free(h->n, status); h->l = oskar_mem_create(prec, OSKAR_CPU, num_pixels, status); h->m = oskar_mem_create(prec, OSKAR_CPU, num_pixels, status); h->n = oskar_mem_create(prec, OSKAR_CPU, num_pixels, status); oskar_evaluate_image_lmn_grid(h->image_size, h->image_size, h->fov_deg * M_PI/180, h->fov_deg * M_PI/180, 0, h->l, h->m, h->n, status); oskar_mem_add_real(h->n, -1.0, status); /* n-1 */ /* Expand the number of devices to the number of selected GPUs, * if required. */ if (h->num_devices < h->num_gpus) oskar_imager_set_num_devices(h, h->num_gpus); /* Initialise device memory. */ for (i = 0; i < h->num_devices; ++i) { DeviceData* d = &h->d[i]; /* Select the device. */ if (i < h->num_gpus) { oskar_device_set(h->gpu_ids[i], status); dev_loc = OSKAR_GPU; } else { dev_loc = OSKAR_CPU; } if (*status) return; if (!d->amp) { d->uu = oskar_mem_create(prec, dev_loc, 0, status); d->vv = oskar_mem_create(prec, dev_loc, 0, status); d->ww = oskar_mem_create(prec, dev_loc, 0, status); d->weight = oskar_mem_create(prec, dev_loc, 0, status); d->amp = oskar_mem_create(prec | OSKAR_COMPLEX, dev_loc, 0, status); } if (!d->block_dev) { d->l = oskar_mem_create(prec, dev_loc, 0, status); d->m = oskar_mem_create(prec, dev_loc, 0, status); d->n = oskar_mem_create(prec, dev_loc, 0, status); d->block_dev = oskar_mem_create(prec, dev_loc, 0, status); d->block_cpu = oskar_mem_create(prec, OSKAR_CPU, 0, status); } if (i < h->num_gpus) oskar_device_synchronize(); } } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/sky/oskar_sky_set_source.h
/* * Copyright (c) 2011-2013, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_SKY_SET_SOURCE_H_ #define OSKAR_SKY_SET_SOURCE_H_ /** * @file oskar_sky_set_source.h */ #include <oskar_global.h> #ifdef __cplusplus extern "C" { #endif /** * @brief * Sets source data into a sky model. * * @details * This function sets sky model data for a single source at the given index. * The sky model must already be large enough to hold the source data. * * @param[in,out] sky Pointer to sky model. * @param[in] index Source index in sky model to set. * @param[in] ra_rad Source right ascension in radians. * @param[in] dec_rad Source declination in radians. * @param[in] I Source Stokes I in Jy. * @param[in] Q Source Stokes Q in Jy. * @param[in] U Source Stokes U in Jy. * @param[in] V Source Stokes V in Jy. * @param[in] ref_frequency_hz Source reference frequency in Hz. * @param[in] spectral_index Source spectral index. * @param[in] rotation_measure Source rotation measure in radians/m^2. * @param[in] fwhm_major_rad Gaussian source major axis FWHM, in radians. * @param[in] fwhm_minor_rad Gaussian source minor axis FWHM, in radians. * @param[in] position_angle_rad Gaussian source position angle, in radians. * @param[in,out] status Status return code. */ OSKAR_EXPORT void oskar_sky_set_source(oskar_Sky* sky, int index, double ra_rad, double dec_rad, double I, double Q, double U, double V, double ref_frequency_hz, double spectral_index, double rotation_measure, double fwhm_major_rad, double fwhm_minor_rad, double position_angle_rad, int* status); #ifdef __cplusplus } #endif #endif /* OSKAR_SKY_SET_SOURCE_H_ */
davepallot/OSKAR
oskar/ms/oskar_ms_read.h
/* * Copyright (c) 2011-2016, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_MS_READ_H_ #define OSKAR_MS_READ_H_ /** * @file oskar_ms_read.h */ #include <oskar_global.h> #ifdef __cplusplus extern "C" { #endif /** * @brief Gets data from one column in a Measurement Set. * * @details * Gets data from one column in a Measurement Set. * * @param[in] p Pointer to opened Measurement Set. * @param[in] column Name of required column in main table. * @param[in] start_row Start row. * @param[in] num_rows Number of rows to return. * @param[in] data_size_bytes Data size of allocated block, in bytes. * @param[in,out] data Data block to fill. * @param[out] required_size_bytes Required size of the data block, in bytes. * @param[in,out] status Status return code. */ OSKAR_MS_EXPORT void oskar_ms_read_column(const oskar_MeasurementSet* p, const char* column, unsigned int start_row, unsigned int num_rows, size_t data_size_bytes, void* data, size_t* required_size_bytes, int* status); /** * @details * Reads baseline coordinate data from the main table. * * @details * This function reads a list of baseline coordinates from * the main table of the Measurement Set. The coordinate arrays must be * allocated to the correct size on entry. * * @param[in] start_row The start row from which to read (zero-based). * @param[in] num_baselines Number of baselines to read from the main table. * @param[in,out] uu Baseline u-coordinates, in metres. * @param[in,out] vv Baseline v-coordinates, in metres. * @param[in,out] ww Baseline w-coordinates, in metres. * @param[in,out] status Status return code. */ OSKAR_MS_EXPORT void oskar_ms_read_coords_d(oskar_MeasurementSet* p, unsigned int start_row, unsigned int num_baselines, double* uu, double* vv, double* ww, int* status); /** * @details * Reads baseline coordinate data from the main table. * * @details * This function reads a list of baseline coordinates from * the main table of the Measurement Set. The coordinate arrays must be * allocated to the correct size on entry. * * @param[in] start_row The start row from which to read (zero-based). * @param[in] num_baselines Number of baselines to read from the main table. * @param[in,out] uu Baseline u-coordinates, in metres. * @param[in,out] vv Baseline v-coordinates, in metres. * @param[in,out] ww Baseline w-coordinates, in metres. * @param[in,out] status Status return code. */ OSKAR_MS_EXPORT void oskar_ms_read_coords_f(oskar_MeasurementSet* p, unsigned int start_row, unsigned int num_baselines, float* uu, float* vv, float* ww, int* status); /** * @details * Reads visibility data from the main table. * * @details * This function reads a block of visibility data from the specified column of * the main table of the Measurement Set. The \p vis array must be * allocated to the correct size on entry. * * The dimensionality of the complex \p vis data block is: * (num_channels * num_baselines * num_pols) * with num_pols the fastest varying dimension, then num_baselines, * and num_channels the slowest. * * @param[in] start_row The start row from which to read (zero-based). * @param[in] start_channel The start channel index to read (zero-based). * @param[in] num_channels Number of channels to read. * @param[in] num_baselines Number of baselines to read from the main table. * @param[in] column Name of column (DATA, MODEL_DATA or CORRECTED_DATA). * @param[in,out] vis Visibility data. * @param[in,out] status Status return code. */ OSKAR_MS_EXPORT void oskar_ms_read_vis_d(oskar_MeasurementSet* p, unsigned int start_row, unsigned int start_channel, unsigned int num_channels, unsigned int num_baselines, const char* column, double* vis, int* status); /** * @details * Reads visibility data from the main table. * * @details * This function reads a block of visibility data from the specified column of * the main table of the Measurement Set. The \p vis array must be * allocated to the correct size on entry. * * The dimensionality of the complex \p vis data block is: * (num_channels * num_baselines * num_pols) * with num_pols the fastest varying dimension, then num_baselines, * and num_channels the slowest. * * @param[in] start_row The start row from which to read (zero-based). * @param[in] start_channel The start channel index to read (zero-based). * @param[in] num_channels Number of channels to read. * @param[in] num_baselines Number of baselines to read from the main table. * @param[in] column Name of column (DATA, MODEL_DATA or CORRECTED_DATA). * @param[in,out] vis Visibility data. * @param[in,out] status Status return code. */ OSKAR_MS_EXPORT void oskar_ms_read_vis_f(oskar_MeasurementSet* p, unsigned int start_row, unsigned int start_channel, unsigned int num_channels, unsigned int num_baselines, const char* column, float* vis, int* status); #ifdef __cplusplus } #endif #endif /* OSKAR_MS_READ_H_ */
davepallot/OSKAR
oskar/math/oskar_multiply_inline.h
/* * Copyright (c) 2013-2018, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_MULTIPLY_INLINE_H_ #define OSKAR_MULTIPLY_INLINE_H_ /** * @file oskar_multiply_inline.h */ #include <oskar_global.h> #ifdef __CUDACC__ /* Must include this first to avoid type conflicts. */ #include <vector_types.h> #endif #include <utility/oskar_vector_types.h> #ifdef __cplusplus extern "C" { #endif /* OUT_S = A * B */ #define OSKAR_MUL_COMPLEX(OUT_S, A, B) { \ OUT_S.x = A.x * B.x - A.y * B.y; \ OUT_S.y = A.x * B.y + A.y * B.x; } /* OUT_S = A * conj(B) */ #define OSKAR_MUL_COMPLEX_CONJUGATE(OUT_S, A, B) { \ OUT_S.x = A.x * B.x + A.y * B.y; \ OUT_S.y = A.y * B.x - A.x * B.y; } /* A *= B */ #define OSKAR_MUL_COMPLEX_IN_PLACE(REAL2, A, B) { \ const REAL2 a1__ = A; \ A.x *= B.x; A.x -= a1__.y * B.y; \ A.y = a1__.x * B.y; A.y += a1__.y * B.x; } /* A *= conj(B) */ #define OSKAR_MUL_COMPLEX_CONJUGATE_IN_PLACE(REAL2, A, B) { \ const REAL2 a1__ = A; \ A.x *= B.x; A.x += a1__.y * B.y; \ A.y = a1__.y * B.x; A.y -= a1__.x * B.y; } /* M *= A */ #define OSKAR_MUL_COMPLEX_MATRIX_COMPLEX_SCALAR_IN_PLACE(REAL2, M, A) { \ OSKAR_MUL_COMPLEX_IN_PLACE(REAL2, M.a, A); \ OSKAR_MUL_COMPLEX_IN_PLACE(REAL2, M.b, A); \ OSKAR_MUL_COMPLEX_IN_PLACE(REAL2, M.c, A); \ OSKAR_MUL_COMPLEX_IN_PLACE(REAL2, M.d, A); } /* M1 = M1 * M2 * a = a1 a2 + b1 c2 * b = a1 b2 + b1 d2 * c = c1 a2 + d1 c2 * d = c1 b2 + d1 d2 */ #define OSKAR_MUL_COMPLEX_MATRIX_IN_PLACE(REAL2, M1, M2) { \ REAL2 t__; const REAL2 a__ = M1.a; const REAL2 c__ = M1.c; \ OSKAR_MUL_COMPLEX_IN_PLACE(REAL2, M1.a, M2.a); \ OSKAR_MUL_COMPLEX(t__, M1.b, M2.c); \ M1.a.x += t__.x; M1.a.y += t__.y; \ OSKAR_MUL_COMPLEX_IN_PLACE(REAL2, M1.c, M2.a); \ OSKAR_MUL_COMPLEX(t__, M1.d, M2.c); \ M1.c.x += t__.x; M1.c.y += t__.y; \ OSKAR_MUL_COMPLEX_IN_PLACE(REAL2, M1.b, M2.d); \ OSKAR_MUL_COMPLEX(t__, a__, M2.b); \ M1.b.x += t__.x; M1.b.y += t__.y; \ OSKAR_MUL_COMPLEX_IN_PLACE(REAL2, M1.d, M2.d); \ OSKAR_MUL_COMPLEX(t__, c__, M2.b); \ M1.d.x += t__.x; M1.d.y += t__.y; } /* M1 = M1 * conj_trans(M2) */ #define OSKAR_MUL_COMPLEX_MATRIX_CONJUGATE_TRANSPOSE_IN_PLACE(REAL2, M1, M2) { \ REAL2 t__; const REAL2 a__ = M1.a; const REAL2 c__ = M1.c; \ OSKAR_MUL_COMPLEX_CONJUGATE_IN_PLACE(REAL2, M1.a, M2.a); \ OSKAR_MUL_COMPLEX_CONJUGATE(t__, M1.b, M2.b); \ M1.a.x += t__.x; M1.a.y += t__.y; \ OSKAR_MUL_COMPLEX_CONJUGATE_IN_PLACE(REAL2, M1.c, M2.a); \ OSKAR_MUL_COMPLEX_CONJUGATE(t__, M1.d, M2.b); \ M1.c.x += t__.x; M1.c.y += t__.y; \ OSKAR_MUL_COMPLEX_CONJUGATE_IN_PLACE(REAL2, M1.b, M2.d); \ OSKAR_MUL_COMPLEX_CONJUGATE(t__, a__, M2.c); \ M1.b.x += t__.x; M1.b.y += t__.y; \ OSKAR_MUL_COMPLEX_CONJUGATE_IN_PLACE(REAL2, M1.d, M2.d); \ OSKAR_MUL_COMPLEX_CONJUGATE(t__, c__, M2.c); \ M1.d.x += t__.x; M1.d.y += t__.y; } /* M1 = M1 * M2 * The second matrix must have a and d both real, with the form: * ( a b ) * ( - d ) */ #define OSKAR_MUL_COMPLEX_MATRIX_HERMITIAN_IN_PLACE(REAL2, M1, M2) { \ REAL2 t__; const REAL2 a__ = M1.a; const REAL2 c__ = M1.c; \ M1.a.x *= M2.a.x; M1.a.y *= M2.a.x; \ OSKAR_MUL_COMPLEX_CONJUGATE(t__, M1.b, M2.b); \ M1.a.x += t__.x; M1.a.y += t__.y; \ M1.c.x *= M2.a.x; M1.c.y *= M2.a.x; \ OSKAR_MUL_COMPLEX_CONJUGATE(t__, M1.d, M2.b); \ M1.c.x += t__.x; M1.c.y += t__.y; \ M1.b.x *= M2.d.x; M1.b.y *= M2.d.x; \ OSKAR_MUL_COMPLEX(t__, a__, M2.b); \ M1.b.x += t__.x; M1.b.y += t__.y; \ M1.d.x *= M2.d.x; M1.d.y *= M2.d.x; \ OSKAR_MUL_COMPLEX(t__, c__, M2.b); \ M1.d.x += t__.x; M1.d.y += t__.y; } /* OUT_S += A * B */ #define OSKAR_MUL_ADD_COMPLEX(OUT_S, A, B) { \ OUT_S.x += A.x * B.x; OUT_S.x -= A.y * B.y; \ OUT_S.y += A.x * B.y; OUT_S.y += A.y * B.x; } /* OUT_S += A * conj(B) */ #define OSKAR_MUL_ADD_COMPLEX_CONJUGATE(OUT_S, A, B) { \ OUT_S.x += A.x * B.x; OUT_S.x += A.y * B.y; \ OUT_S.y += A.y * B.x; OUT_S.y -= A.x * B.y; } /* OUT_M = M * A */ #define OSKAR_MUL_COMPLEX_MATRIX_COMPLEX_SCALAR(OUT_M, M, A) { \ OSKAR_MUL_COMPLEX(OUT_M.a, M.a, A) \ OSKAR_MUL_COMPLEX(OUT_M.b, M.b, A) \ OSKAR_MUL_COMPLEX(OUT_M.c, M.c, A) \ OSKAR_MUL_COMPLEX(OUT_M.d, M.d, A) } /* OUT_M += M1 * A */ #define OSKAR_MUL_ADD_COMPLEX_MATRIX_SCALAR(OUT_M, M1, A) { \ OUT_M.a.x += M1.a.x * A; OUT_M.a.y += M1.a.y * A; \ OUT_M.b.x += M1.b.x * A; OUT_M.b.y += M1.b.y * A; \ OUT_M.c.x += M1.c.x * A; OUT_M.c.y += M1.c.y * A; \ OUT_M.d.x += M1.d.x * A; OUT_M.d.y += M1.d.y * A; } /* OUT_M = M1 * M2 * a = a1 a2 + b1 c2 * b = a1 b2 + b1 d2 * c = c1 a2 + d1 c2 * d = c1 b2 + d1 d2 */ #define OSKAR_MUL_COMPLEX_MATRIX(OUT_M, M1, M2) { \ OSKAR_MUL_COMPLEX(OUT_M.a, M1.a, M2.a) \ OSKAR_MUL_COMPLEX(OUT_M.b, M1.a, M2.b) \ OSKAR_MUL_COMPLEX(OUT_M.c, M1.c, M2.a) \ OSKAR_MUL_COMPLEX(OUT_M.d, M1.c, M2.b) \ OSKAR_MUL_ADD_COMPLEX(OUT_M.a, M1.b, M2.c) \ OSKAR_MUL_ADD_COMPLEX(OUT_M.b, M1.b, M2.d) \ OSKAR_MUL_ADD_COMPLEX(OUT_M.c, M1.d, M2.c) \ OSKAR_MUL_ADD_COMPLEX(OUT_M.d, M1.d, M2.d) /* OUT_M = M1 * conj_trans(M2) * a = a1 a2* + b1 b2* * b = a1 c2* + b1 d2* * c = c1 a2* + d1 b2* * d = c1 c2* + d1 d2* */ #define OSKAR_MUL_COMPLEX_MATRIX_CONJUGATE_TRANSPOSE(OUT_M, M1, M2) { \ OSKAR_MUL_COMPLEX_CONJUGATE(OUT_M.a, M1.a, M2.a) \ OSKAR_MUL_COMPLEX_CONJUGATE(OUT_M.b, M1.a, M2.c) \ OSKAR_MUL_COMPLEX_CONJUGATE(OUT_M.c, M1.c, M2.a) \ OSKAR_MUL_COMPLEX_CONJUGATE(OUT_M.d, M1.c, M2.c) \ OSKAR_MUL_ADD_COMPLEX_CONJUGATE(OUT_M.a, M1.b, M2.b) \ OSKAR_MUL_ADD_COMPLEX_CONJUGATE(OUT_M.b, M1.b, M2.d) \ OSKAR_MUL_ADD_COMPLEX_CONJUGATE(OUT_M.c, M1.d, M2.b) \ OSKAR_MUL_ADD_COMPLEX_CONJUGATE(OUT_M.d, M1.d, M2.d) } /* OUT_V = M * IN */ #define OSKAR_MUL_3X3_MATRIX_VECTOR(OUT_V, M, V) { \ OUT_V[0] = M[0] * V[0] + M[1] * V[1] + M[2] * V[2]; \ OUT_V[1] = M[3] * V[0] + M[4] * V[1] + M[5] * V[2]; \ OUT_V[2] = M[6] * V[0] + M[7] * V[1] + M[8] * V[2]; } /** * @brief * Multiplies two complex numbers (single precision). * * @details * This function multiplies two complex numbers. * * @param[out] out The output complex number. * @param[in] a The first complex number. * @param[in] b The second complex number. */ OSKAR_INLINE void oskar_multiply_complex_f(float2* out, const float2* a, const float2* b) { out->x = a->x * b->x; out->y = a->x * b->y; out->x -= a->y * b->y; /* RE*RE - IM*IM */ out->y += a->y * b->x; /* RE*IM + IM*RE */ } /** * @brief * Multiplies two complex numbers (double precision). * * @details * This function multiplies two complex numbers. * * @param[out] out The output complex number. * @param[in] a The first complex number. * @param[in] b The second complex number. */ OSKAR_INLINE void oskar_multiply_complex_d(double2* out, const double2* a, const double2* b) { out->x = a->x * b->x; out->y = a->x * b->y; out->x -= a->y * b->y; /* RE*RE - IM*IM */ out->y += a->y * b->x; /* RE*IM + IM*RE */ } /** * @brief * Multiplies two complex numbers in-place (single precision). * * @details * This function multiplies two complex numbers, overwriting the first. * * @param[in,out] a On input, the first complex number; on output, the result. * @param[in] b The second complex number. */ OSKAR_INLINE void oskar_multiply_complex_in_place_f(float2* a, const float2* b) { /* Copy input a. */ float2 a1; a1 = *a; /* Multiply complex numbers: a = a * b. */ a->x *= b->x; a->y = a1.x * b->y; a->x -= a1.y * b->y; /* RE*RE - IM*IM */ a->y += a1.y * b->x; /* RE*IM + IM*RE */ } /** * @brief * Multiplies two complex numbers in-place (double precision). * * @details * This function multiplies two complex numbers, overwriting the first. * * @param[in,out] a On input, the first complex number; on output, the result. * @param[in] b The second complex number. */ OSKAR_INLINE void oskar_multiply_complex_in_place_d(double2* a, const double2* b) { /* Copy input a. */ double2 a1; a1 = *a; /* Multiply complex numbers: a = a * b. */ a->x *= b->x; a->y = a1.x * b->y; a->x -= a1.y * b->y; /* RE*RE - IM*IM */ a->y += a1.y * b->x; /* RE*IM + IM*RE */ } /** * @brief * Multiplies two complex numbers in-place (single precision). * * @details * This function multiplies two complex numbers, taking the complex conjugate * of the second, and overwriting the first. * * @param[in,out] a On input, the first complex number; on output, the result. * @param[in] b The second complex number. */ OSKAR_INLINE void oskar_multiply_complex_conjugate_in_place_f(float2* a, const float2* b) { /* Copy input a. */ float2 a1; a1 = *a; /* Multiply complex numbers: a = a * conjugate(b). */ a->x *= b->x; a->y = a1.y * b->x; a->x += a1.y * b->y; /* RE*RE + IM*IM */ a->y -= a1.x * b->y; /* IM*RE - RE*IM */ } /** * @brief * Multiplies two complex numbers in-place (double precision). * * @details * This function multiplies two complex numbers, taking the complex conjugate * of the second, and overwriting the first. * * @param[in,out] a On input, the first complex number; on output, the result. * @param[in] b The second complex number. */ OSKAR_INLINE void oskar_multiply_complex_conjugate_in_place_d(double2* a, const double2* b) { /* Copy input a. */ double2 a1; a1 = *a; /* Multiply complex numbers: a = a * conjugate(b). */ a->x *= b->x; a->y = a1.y * b->x; a->x += a1.y * b->y; /* RE*RE + IM*IM */ a->y -= a1.x * b->y; /* IM*RE - RE*IM */ } /** * @brief * Multiplies a complex matrix and a complex scalar in-place (single precision). * * @details * This function multiplies a complex matrix and a complex scalar, overwriting * the input matrix. * * @param[in,out] m On input, the complex matrix; on output, the result. * @param[in] a The complex scalar number. */ OSKAR_INLINE void oskar_multiply_complex_matrix_complex_scalar_in_place_f(float4c* m, const float2* a) { oskar_multiply_complex_in_place_f(&m->a, a); oskar_multiply_complex_in_place_f(&m->b, a); oskar_multiply_complex_in_place_f(&m->c, a); oskar_multiply_complex_in_place_f(&m->d, a); } /** * @brief * Multiplies a complex matrix and a complex scalar in-place (double precision). * * @details * This function multiplies a complex matrix and a complex scalar, overwriting * the input matrix. * * @param[in,out] m On input, the complex matrix; on output, the result. * @param[in] a The complex scalar number. */ OSKAR_INLINE void oskar_multiply_complex_matrix_complex_scalar_in_place_d(double4c* m, const double2* a) { oskar_multiply_complex_in_place_d(&m->a, a); oskar_multiply_complex_in_place_d(&m->b, a); oskar_multiply_complex_in_place_d(&m->c, a); oskar_multiply_complex_in_place_d(&m->d, a); } /** * @brief * Multiplies two complex matrices in-place (single precision). * * @details * This function multiplies two complex matrices, overwriting the first. * Matrix multiplication is done in the order M1 = M1 * M2. * * @param[in,out] m1 On input, the first complex matrix; on output, the result. * @param[in] m2 The second complex matrix. */ OSKAR_INLINE void oskar_multiply_complex_matrix_in_place_f(float4c* m1, const float4c* m2) { /* Copy a and c from the input matrix. */ float2 a, c, t; a = m1->a; c = m1->c; oskar_multiply_complex_in_place_f(&m1->a, &m2->a); /* a = a1 a2 + b1 c2 */ oskar_multiply_complex_f(&t, &m1->b, &m2->c); m1->a.x += t.x; /* Real part. */ m1->a.y += t.y; /* Imag part. */ oskar_multiply_complex_in_place_f(&m1->c, &m2->a); /* c = c1 a2 + d1 c2 */ oskar_multiply_complex_f(&t, &m1->d, &m2->c); m1->c.x += t.x; /* Real part. */ m1->c.y += t.y; /* Imag part. */ oskar_multiply_complex_in_place_f(&m1->b, &m2->d); /* b = a1 b2 + b1 d2 */ oskar_multiply_complex_f(&t, &a, &m2->b); m1->b.x += t.x; /* Real part. */ m1->b.y += t.y; /* Imag part. */ oskar_multiply_complex_in_place_f(&m1->d, &m2->d); /* d = c1 b2 + d1 d2 */ oskar_multiply_complex_f(&t, &c, &m2->b); m1->d.x += t.x; /* Real part. */ m1->d.y += t.y; /* Imag part. */ } /** * @brief * Multiplies two complex matrices in-place (double precision). * * @details * This function multiplies two complex matrices, overwriting the first. * Matrix multiplication is done in the order M1 = M1 * M2. * * @param[in,out] m1 On input, the first complex matrix; on output, the result. * @param[in] m2 The second complex matrix. */ OSKAR_INLINE void oskar_multiply_complex_matrix_in_place_d(double4c* m1, const double4c* m2) { /* Copy a and c from the input matrix. */ double2 a, c, t; a = m1->a; c = m1->c; oskar_multiply_complex_in_place_d(&m1->a, &m2->a); /* a = a1 a2 + b1 c2 */ oskar_multiply_complex_d(&t, &m1->b, &m2->c); m1->a.x += t.x; /* Real part. */ m1->a.y += t.y; /* Imag part. */ oskar_multiply_complex_in_place_d(&m1->c, &m2->a); /* c = c1 a2 + d1 c2 */ oskar_multiply_complex_d(&t, &m1->d, &m2->c); m1->c.x += t.x; /* Real part. */ m1->c.y += t.y; /* Imag part. */ oskar_multiply_complex_in_place_d(&m1->b, &m2->d); /* b = a1 b2 + b1 d2 */ oskar_multiply_complex_d(&t, &a, &m2->b); m1->b.x += t.x; /* Real part. */ m1->b.y += t.y; /* Imag part. */ oskar_multiply_complex_in_place_d(&m1->d, &m2->d); /* d = c1 b2 + d1 d2 */ oskar_multiply_complex_d(&t, &c, &m2->b); m1->d.x += t.x; /* Real part. */ m1->d.y += t.y; /* Imag part. */ } #ifdef __cplusplus } #endif #endif /* OSKAR_MULTIPLY_INLINE_H_ */
davepallot/OSKAR
oskar/utility/oskar_device_utils.h
<reponame>davepallot/OSKAR<filename>oskar/utility/oskar_device_utils.h<gh_stars>1-10 /* * Copyright (c) 2012-2018, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_DEVICE_UTILS_H_ #define OSKAR_DEVICE_UTILS_H_ /** * @file oskar_device_utils.h */ #include <oskar_global.h> #include <stddef.h> #ifdef __cplusplus extern "C" { #endif /** * @brief Checks if a device error occurred. * * @details * This function checks to see if a device error occurred. * * @param[out] status Status return code. */ OSKAR_EXPORT void oskar_device_check_error(int* status); /** * @brief Returns the CUDA compute capability of the current device. * * @details * The returned value is calculated as (10 * major_version + minor_version). * If CUDA is not available, this will be 0. */ OSKAR_EXPORT int oskar_device_compute_capability(void); /** * @brief * Returns the number of devices on the system. * * @details * This simply calls cudaGetDeviceCount() if CUDA is available. */ OSKAR_EXPORT int oskar_device_count(int* status); /** * @brief * Returns the amount of free memory on the device. * * @details * This simply calls cudaMemGetInfo() if CUDA is available. */ OSKAR_EXPORT void oskar_device_mem_info(size_t* mem_free, size_t* mem_total); /** * @brief * Returns the name of the specified device. * * @details * The name of the device specified by \p device_id is returned as a string. * Use free() to deallocate the string. */ OSKAR_EXPORT char* oskar_device_name(int device_id); /** * @brief * Resets the current device. * * @details * This simply calls cudaDeviceReset() if CUDA is available. */ OSKAR_EXPORT void oskar_device_reset(void); /** * @brief * Sets the device to use for subsequent device calls. * * @details * Sets the device to use for subsequent device calls. * This simply calls cudaSetDevice() if CUDA is available. * * @param[in] id CUDA device ID. * @param[in,out] status Status return code. */ OSKAR_EXPORT void oskar_device_set(int id, int* status); /** * @brief * Synchronises the current device. * * @details * This simply calls cudaDeviceSynchronize() if CUDA is available. */ OSKAR_EXPORT void oskar_device_synchronize(void); #ifdef __cplusplus } #endif #endif /* OSKAR_DEVICE_UTILS_H_ */
davepallot/OSKAR
oskar/convert/private_convert_ludwig3_to_theta_phi_components_inline.h
/* * Copyright (c) 2014, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_CONVERT_LUDWIG3_TO_THETA_PHI_COMPONENTS_INLINE_H_ #define OSKAR_CONVERT_LUDWIG3_TO_THETA_PHI_COMPONENTS_INLINE_H_ #include <oskar_global.h> #include <math.h> #ifdef __cplusplus extern "C" { #endif /* Single precision. */ OSKAR_INLINE void oskar_convert_ludwig3_to_theta_phi_components_inline_f( float2* h_theta, float2* v_phi, const float phi) { float sin_p, cos_p; float2 E_h, E_v; /* Sine and cosine of phi. */ #ifdef __CUDACC__ sincosf(phi, &sin_p, &cos_p); #else sin_p = sinf(phi); cos_p = cosf(phi); #endif /* Copy input Ludwig-3 components. */ E_h.x = h_theta->x; E_h.y = h_theta->y; E_v.x = v_phi->x; E_v.y = v_phi->y; /* Matrix transform for both real and imaginary parts. */ h_theta->x = E_h.x * cos_p + E_v.x * sin_p; h_theta->y = E_h.y * cos_p + E_v.y * sin_p; v_phi->x = -E_h.x * sin_p + E_v.x * cos_p; v_phi->y = -E_h.y * sin_p + E_v.y * cos_p; } /* Double precision. */ OSKAR_INLINE void oskar_convert_ludwig3_to_theta_phi_components_inline_d( double2* h_theta, double2* v_phi, const double phi) { double sin_p, cos_p; double2 E_h, E_v; /* Sine and cosine of phi. */ #ifdef __CUDACC__ sincos(phi, &sin_p, &cos_p); #else sin_p = sin(phi); cos_p = cos(phi); #endif /* Copy input Ludwig-3 components. */ E_h.x = h_theta->x; E_h.y = h_theta->y; E_v.x = v_phi->x; E_v.y = v_phi->y; /* Matrix transform for both real and imaginary parts. */ h_theta->x = E_h.x * cos_p + E_v.x * sin_p; h_theta->y = E_h.y * cos_p + E_v.y * sin_p; v_phi->x = -E_h.x * sin_p + E_v.x * cos_p; v_phi->y = -E_h.y * sin_p + E_v.y * cos_p; } #ifdef __cplusplus } #endif #endif /* OSKAR_CONVERT_LUDWIG3_TO_THETA_PHI_COMPONENTS_INLINE_H_ */
davepallot/OSKAR
oskar/utility/src/oskar_timer.c
<filename>oskar/utility/src/oskar_timer.c /* * Copyright (c) 2013-2018, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "utility/oskar_timer.h" #include "utility/oskar_thread.h" #include <stdlib.h> #include <stdio.h> #include <time.h> #ifndef OSKAR_OS_WIN #include <sys/time.h> #include <unistd.h> #else #include <windows.h> #endif #ifdef OSKAR_HAVE_CUDA #include <cuda_runtime_api.h> #endif #ifdef __cplusplus extern "C" { #endif struct oskar_Timer { int type; int paused; double elapsed; double start; oskar_Mutex* mutex; #ifdef OSKAR_HAVE_CUDA cudaEvent_t start_cuda; cudaEvent_t end_cuda; #endif #ifdef OSKAR_OS_WIN double freq; #endif }; static double oskar_get_wtime(oskar_Timer* timer) { /* Declarations first (needs separate ifdef block). */ #ifdef OSKAR_OS_WIN LARGE_INTEGER cntr; #else #if _POSIX_MONOTONIC_CLOCK > 0 struct timespec ts; #else struct timeval tv; #endif #endif /* Return immediately if timer is not of native type. */ if (timer->type != OSKAR_TIMER_NATIVE) return 0.0; #ifdef OSKAR_OS_WIN /* Windows-specific version. */ QueryPerformanceCounter(&cntr); return (double)(cntr.QuadPart) / timer->freq; #else #if _POSIX_MONOTONIC_CLOCK > 0 /* Use monotonic clock if available. */ clock_gettime(CLOCK_MONOTONIC, &ts); return ts.tv_sec + ts.tv_nsec / 1e9; #else /* Use gettimeofday() as fallback. */ gettimeofday(&tv, 0); return tv.tv_sec + tv.tv_usec / 1e6; #endif #endif } oskar_Timer* oskar_timer_create(int type) { oskar_Timer* timer; #ifdef OSKAR_OS_WIN LARGE_INTEGER freq; #endif /* Create the structure. */ timer = (oskar_Timer*) calloc(1, sizeof(oskar_Timer)); timer->mutex = oskar_mutex_create(); #ifdef OSKAR_OS_WIN QueryPerformanceFrequency(&freq); timer->freq = (double)(freq.QuadPart); #endif timer->type = type; timer->paused = 1; timer->elapsed = 0.0; timer->start = 0.0; #ifdef OSKAR_HAVE_CUDA if (timer->type == OSKAR_TIMER_CUDA) { cudaEventCreate(&timer->start_cuda); cudaEventCreate(&timer->end_cuda); } #endif return timer; } void oskar_timer_free(oskar_Timer* timer) { if (!timer) return; #ifdef OSKAR_HAVE_CUDA if (timer->type == OSKAR_TIMER_CUDA) { cudaEventDestroy(timer->start_cuda); cudaEventDestroy(timer->end_cuda); } #endif oskar_mutex_free(timer->mutex); free(timer); } double oskar_timer_elapsed(oskar_Timer* timer) { double now = 0.0; /* If timer is paused, return immediately with current elapsed time. */ if (timer->paused) return timer->elapsed; #ifdef OSKAR_HAVE_CUDA /* CUDA timer. */ if (timer->type == OSKAR_TIMER_CUDA) { float millisec = 0.0f; oskar_mutex_lock(timer->mutex); /* Get elapsed time since start. */ cudaEventRecord(timer->end_cuda, 0); cudaEventSynchronize(timer->end_cuda); cudaEventElapsedTime(&millisec, timer->start_cuda, timer->end_cuda); /* Increment elapsed time. */ timer->elapsed += millisec / 1000.0; /* Restart. */ cudaEventRecord(timer->start_cuda, 0); oskar_mutex_unlock(timer->mutex); return timer->elapsed; } #endif /* Native timer. */ oskar_mutex_lock(timer->mutex); if (timer->type == OSKAR_TIMER_NATIVE) now = oskar_get_wtime(timer); /* Increment elapsed time and restart. */ timer->elapsed += (now - timer->start); timer->start = now; oskar_mutex_unlock(timer->mutex); return timer->elapsed; } void oskar_timer_pause(oskar_Timer* timer) { if (timer->paused) return; (void)oskar_timer_elapsed(timer); timer->paused = 1; } void oskar_timer_resume(oskar_Timer* timer) { if (!timer->paused) return; oskar_timer_restart(timer); } void oskar_timer_restart(oskar_Timer* timer) { timer->paused = 0; #ifdef OSKAR_HAVE_CUDA /* CUDA timer. */ if (timer->type == OSKAR_TIMER_CUDA) { cudaEventRecord(timer->start_cuda, 0); return; } #endif /* Native timer. */ timer->start = oskar_get_wtime(timer); } void oskar_timer_start(oskar_Timer* timer) { timer->elapsed = 0.0; oskar_timer_restart(timer); } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/telescope/station/src/oskar_station_accessors.c
/* * Copyright (c) 2013-2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "telescope/station/private_station.h" #include "telescope/station/oskar_station_accessors.h" #ifdef __cplusplus extern "C" { #endif /* Data common to all station types. */ int oskar_station_unique_id(const oskar_Station* model) { return model->unique_id; } int oskar_station_precision(const oskar_Station* model) { return model->precision; } int oskar_station_mem_location(const oskar_Station* model) { return model->mem_location; } int oskar_station_type(const oskar_Station* model) { return model->station_type; } int oskar_station_normalise_final_beam(const oskar_Station* model) { return model->normalise_final_beam; } double oskar_station_lon_rad(const oskar_Station* model) { return model->lon_rad; } double oskar_station_lat_rad(const oskar_Station* model) { return model->lat_rad; } double oskar_station_alt_metres(const oskar_Station* model) { return model->alt_metres; } double oskar_station_polar_motion_x_rad(const oskar_Station* model) { return model->pm_x_rad; } double oskar_station_polar_motion_y_rad(const oskar_Station* model) { return model->pm_y_rad; } double oskar_station_beam_lon_rad(const oskar_Station* model) { return model->beam_lon_rad; } double oskar_station_beam_lat_rad(const oskar_Station* model) { return model->beam_lat_rad; } int oskar_station_beam_coord_type(const oskar_Station* model) { return model->beam_coord_type; } oskar_Mem* oskar_station_noise_freq_hz(oskar_Station* model) { return model->noise_freq_hz; } const oskar_Mem* oskar_station_noise_freq_hz_const(const oskar_Station* model) { return model->noise_freq_hz; } oskar_Mem* oskar_station_noise_rms_jy(oskar_Station* model) { return model->noise_rms_jy; } const oskar_Mem* oskar_station_noise_rms_jy_const(const oskar_Station* model) { return model->noise_rms_jy; } /* Data used only for Gaussian beam stations. */ double oskar_station_gaussian_beam_fwhm_rad(const oskar_Station* model) { return model->gaussian_beam_fwhm_rad; } double oskar_station_gaussian_beam_reference_freq_hz(const oskar_Station* model) { return model->gaussian_beam_reference_freq_hz; } /* Data used only for aperture array stations. */ int oskar_station_identical_children(const oskar_Station* model) { return model->identical_children; } int oskar_station_num_elements(const oskar_Station* model) { return model->num_elements; } int oskar_station_num_element_types(const oskar_Station* model) { return model->num_element_types; } int oskar_station_normalise_array_pattern(const oskar_Station* model) { return model->normalise_array_pattern; } int oskar_station_enable_array_pattern(const oskar_Station* model) { return model->enable_array_pattern; } int oskar_station_common_element_orientation(const oskar_Station* model) { return model->common_element_orientation; } int oskar_station_array_is_3d(const oskar_Station* model) { return model->array_is_3d; } int oskar_station_apply_element_errors(const oskar_Station* model) { return model->apply_element_errors; } int oskar_station_apply_element_weight(const oskar_Station* model) { return model->apply_element_weight; } unsigned int oskar_station_seed_time_variable_errors(const oskar_Station* model) { return model->seed_time_variable_errors; } double oskar_station_element_x_alpha_rad(const oskar_Station* model, int index) { return ((const double*) oskar_mem_void_const(model->element_x_alpha_cpu))[index]; } double oskar_station_element_x_beta_rad(const oskar_Station* model, int index) { return ((const double*) oskar_mem_void_const(model->element_x_beta_cpu))[index]; } double oskar_station_element_x_gamma_rad(const oskar_Station* model, int index) { return ((const double*) oskar_mem_void_const(model->element_x_gamma_cpu))[index]; } double oskar_station_element_y_alpha_rad(const oskar_Station* model, int index) { return ((const double*) oskar_mem_void_const(model->element_y_alpha_cpu))[index]; } double oskar_station_element_y_beta_rad(const oskar_Station* model, int index) { return ((const double*) oskar_mem_void_const(model->element_y_beta_cpu))[index]; } double oskar_station_element_y_gamma_rad(const oskar_Station* model, int index) { return ((const double*) oskar_mem_void_const(model->element_y_gamma_cpu))[index]; } oskar_Mem* oskar_station_element_true_x_enu_metres(oskar_Station* model) { return model->element_true_x_enu_metres; } const oskar_Mem* oskar_station_element_true_x_enu_metres_const( const oskar_Station* model) { return model->element_true_x_enu_metres; } oskar_Mem* oskar_station_element_true_y_enu_metres(oskar_Station* model) { return model->element_true_y_enu_metres; } const oskar_Mem* oskar_station_element_true_y_enu_metres_const( const oskar_Station* model) { return model->element_true_y_enu_metres; } oskar_Mem* oskar_station_element_true_z_enu_metres(oskar_Station* model) { return model->element_true_z_enu_metres; } const oskar_Mem* oskar_station_element_true_z_enu_metres_const( const oskar_Station* model) { return model->element_true_z_enu_metres; } oskar_Mem* oskar_station_element_measured_x_enu_metres(oskar_Station* model) { return model->element_measured_x_enu_metres; } const oskar_Mem* oskar_station_element_measured_x_enu_metres_const( const oskar_Station* model) { return model->element_measured_x_enu_metres; } oskar_Mem* oskar_station_element_measured_y_enu_metres(oskar_Station* model) { return model->element_measured_y_enu_metres; } const oskar_Mem* oskar_station_element_measured_y_enu_metres_const( const oskar_Station* model) { return model->element_measured_y_enu_metres; } oskar_Mem* oskar_station_element_measured_z_enu_metres(oskar_Station* model) { return model->element_measured_z_enu_metres; } const oskar_Mem* oskar_station_element_measured_z_enu_metres_const( const oskar_Station* model) { return model->element_measured_z_enu_metres; } oskar_Mem* oskar_station_element_gain(oskar_Station* model) { return model->element_gain; } const oskar_Mem* oskar_station_element_gain_const(const oskar_Station* model) { return model->element_gain; } oskar_Mem* oskar_station_element_gain_error(oskar_Station* model) { return model->element_gain_error; } const oskar_Mem* oskar_station_element_gain_error_const( const oskar_Station* model) { return model->element_gain_error; } oskar_Mem* oskar_station_element_phase_offset_rad(oskar_Station* model) { return model->element_phase_offset_rad; } const oskar_Mem* oskar_station_element_phase_offset_rad_const( const oskar_Station* model) { return model->element_phase_offset_rad; } oskar_Mem* oskar_station_element_phase_error_rad(oskar_Station* model) { return model->element_phase_error_rad; } const oskar_Mem* oskar_station_element_phase_error_rad_const( const oskar_Station* model) { return model->element_phase_error_rad; } oskar_Mem* oskar_station_element_weight(oskar_Station* model) { return model->element_weight; } const oskar_Mem* oskar_station_element_weight_const(const oskar_Station* model) { return model->element_weight; } oskar_Mem* oskar_station_element_types(oskar_Station* model) { return model->element_types; } const oskar_Mem* oskar_station_element_types_const(const oskar_Station* model) { return model->element_types; } const int* oskar_station_element_types_cpu_const(const oskar_Station* model) { return (const int*) oskar_mem_void_const(model->element_types_cpu); } const char* oskar_station_element_mount_types_const(const oskar_Station* model) { return oskar_mem_char_const(model->element_mount_types_cpu); } int oskar_station_has_child(const oskar_Station* model) { return model->child ? 1 : 0; } oskar_Station* oskar_station_child(oskar_Station* model, int i) { return model->child[i]; } const oskar_Station* oskar_station_child_const(const oskar_Station* model, int i) { return model->child[i]; } int oskar_station_has_element(const oskar_Station* model) { return model->element ? 1 : 0; } oskar_Element* oskar_station_element(oskar_Station* model, int element_type_index) { return model->element[element_type_index]; } const oskar_Element* oskar_station_element_const(const oskar_Station* model, int element_type_index) { return model->element[element_type_index]; } int oskar_station_num_permitted_beams(const oskar_Station* model) { return model->num_permitted_beams; } const oskar_Mem* oskar_station_permitted_beam_az_rad_const( const oskar_Station* model) { return model->permitted_beam_az_rad; } const oskar_Mem* oskar_station_permitted_beam_el_rad_const( const oskar_Station* model) { return model->permitted_beam_el_rad; } /* Setters. */ void oskar_station_set_unique_ids(oskar_Station* model, int* counter) { model->unique_id = (*counter)++; if (model->child) { int i; for (i = 0; i < model->num_elements; ++i) oskar_station_set_unique_ids(model->child[i], counter); } } void oskar_station_set_station_type(oskar_Station* model, int type) { model->station_type = type; } void oskar_station_set_normalise_final_beam(oskar_Station* model, int value) { model->normalise_final_beam = value; } void oskar_station_set_position(oskar_Station* model, double longitude_rad, double latitude_rad, double altitude_m) { model->lon_rad = longitude_rad; model->lat_rad = latitude_rad; model->alt_metres = altitude_m; } void oskar_station_set_polar_motion(oskar_Station* model, double pm_x_rad, double pm_y_rad) { int i; model->pm_x_rad = pm_x_rad; model->pm_y_rad = pm_y_rad; /* Set recursively for all child stations. */ if (oskar_station_has_child(model)) { for (i = 0; i < model->num_elements; ++i) { oskar_station_set_polar_motion(model->child[i], pm_x_rad, pm_y_rad); } } } void oskar_station_set_phase_centre(oskar_Station* model, int beam_coord_type, double beam_longitude_rad, double beam_latitude_rad) { model->beam_coord_type = beam_coord_type; model->beam_lon_rad = beam_longitude_rad; model->beam_lat_rad = beam_latitude_rad; } void oskar_station_set_gaussian_beam_values(oskar_Station* model, double fwhm_rad, double ref_freq_hz) { model->gaussian_beam_fwhm_rad = fwhm_rad; model->gaussian_beam_reference_freq_hz = ref_freq_hz; } void oskar_station_set_normalise_array_pattern(oskar_Station* model, int value) { model->normalise_array_pattern = value; } void oskar_station_set_enable_array_pattern(oskar_Station* model, int value) { model->enable_array_pattern = value; } void oskar_station_set_seed_time_variable_errors(oskar_Station* model, unsigned int value) { model->seed_time_variable_errors = value; } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/imager/src/private_imager_read_coords.c
/* * Copyright (c) 2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "imager/private_imager.h" #include "imager/private_imager_read_coords.h" #include "imager/oskar_imager.h" #include "binary/oskar_binary.h" #include "math/oskar_cmath.h" #include "mem/oskar_binary_read_mem.h" #include "ms/oskar_measurement_set.h" #include "vis/oskar_vis_block.h" #include "vis/oskar_vis_header.h" #include "utility/oskar_timer.h" #include <float.h> #include <stdlib.h> #ifdef __cplusplus extern "C" { #endif void oskar_imager_read_coords_ms(oskar_Imager* h, const char* filename, int i_file, int num_files, int* percent_done, int* percent_next, int* status) { #ifndef OSKAR_NO_MS oskar_MeasurementSet* ms; oskar_Mem *uvw, *u, *v, *w, *weight, *time_centroid; int num_channels, num_stations, num_baselines, num_pols; int start_row, num_rows; double *uvw_, *u_, *v_, *w_; if (*status) return; /* Read the header. */ ms = oskar_ms_open(filename); if (!ms) { *status = OSKAR_ERR_FILE_IO; return; } num_rows = (int) oskar_ms_num_rows(ms); num_stations = (int) oskar_ms_num_stations(ms); num_baselines = num_stations * (num_stations - 1) / 2; num_pols = (int) oskar_ms_num_pols(ms); num_channels = (int) oskar_ms_num_channels(ms); /* Set visibility meta-data. */ oskar_imager_set_vis_frequency(h, oskar_ms_freq_start_hz(ms), oskar_ms_freq_inc_hz(ms), num_channels); oskar_imager_set_vis_phase_centre(h, oskar_ms_phase_centre_ra_rad(ms) * 180/M_PI, oskar_ms_phase_centre_dec_rad(ms) * 180/M_PI); /* Create arrays. */ uvw = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, 3 * num_baselines, status); u = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, num_baselines, status); v = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, num_baselines, status); w = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, num_baselines, status); weight = oskar_mem_create(OSKAR_SINGLE, OSKAR_CPU, num_baselines * num_pols, status); time_centroid = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, num_baselines, status); uvw_ = oskar_mem_double(uvw, status); u_ = oskar_mem_double(u, status); v_ = oskar_mem_double(v, status); w_ = oskar_mem_double(w, status); /* Loop over visibility blocks. */ for (start_row = 0; start_row < num_rows; start_row += num_baselines) { int i, block_size; size_t allocated, required; if (*status) break; /* Read coordinates and weights from Measurement Set. */ oskar_timer_resume(h->tmr_read); block_size = num_rows - start_row; if (block_size > num_baselines) block_size = num_baselines; allocated = oskar_mem_length(uvw) * oskar_mem_element_size(oskar_mem_type(uvw)); oskar_ms_read_column(ms, "UVW", start_row, block_size, allocated, oskar_mem_void(uvw), &required, status); allocated = oskar_mem_length(weight) * oskar_mem_element_size(oskar_mem_type(weight)); oskar_ms_read_column(ms, "WEIGHT", start_row, block_size, allocated, oskar_mem_void(weight), &required, status); allocated = oskar_mem_length(time_centroid) * oskar_mem_element_size(oskar_mem_type(time_centroid)); oskar_ms_read_column(ms, "TIME_CENTROID", start_row, block_size, allocated, oskar_mem_void(time_centroid), &required, status); /* Split up baseline coordinates. */ for (i = 0; i < block_size; ++i) { u_[i] = uvw_[3*i + 0]; v_[i] = uvw_[3*i + 1]; w_[i] = uvw_[3*i + 2]; } /* Update the imager with the data. */ oskar_timer_pause(h->tmr_read); oskar_imager_update(h, block_size, 0, num_channels - 1, num_pols, u, v, w, 0, weight, time_centroid, status); *percent_done = (int) round(100.0 * ( (start_row + block_size) / (double)(num_rows * num_files) + i_file / (double)num_files)); if (h->log && percent_next && *percent_done >= *percent_next) { oskar_log_message(h->log, 'S', -2, "%3d%% ...", *percent_done); *percent_next = 10 + 10 * (*percent_done / 10); } } oskar_mem_free(uvw, status); oskar_mem_free(u, status); oskar_mem_free(v, status); oskar_mem_free(w, status); oskar_mem_free(weight, status); oskar_mem_free(time_centroid, status); oskar_ms_close(ms); #else (void) filename; (void) i_file; (void) num_files; (void) percent_done; (void) percent_next; oskar_log_error(h->log, "OSKAR was compiled without Measurement Set support."); *status = OSKAR_ERR_FUNCTION_NOT_AVAILABLE; #endif } void oskar_imager_read_coords_vis(oskar_Imager* h, const char* filename, int i_file, int num_files, int* percent_done, int* percent_next, int* status) { oskar_Binary* vis_file; oskar_VisHeader* header; oskar_Mem *uu, *vv, *ww, *weight, *time_centroid, *time_slice; int coord_prec, max_times_per_block, tags_per_block, i_block, num_blocks; int num_times_total, num_stations, num_baselines, num_pols; double time_start_mjd, time_inc_sec; if (*status) return; /* Read the header. */ vis_file = oskar_binary_create(filename, 'r', status); header = oskar_vis_header_read(vis_file, status); if (*status) { oskar_vis_header_free(header, status); oskar_binary_free(vis_file); return; } coord_prec = oskar_vis_header_coord_precision(header); max_times_per_block = oskar_vis_header_max_times_per_block(header); tags_per_block = oskar_vis_header_num_tags_per_block(header); num_times_total = oskar_vis_header_num_times_total(header); num_stations = oskar_vis_header_num_stations(header); num_baselines = num_stations * (num_stations - 1) / 2; num_pols = oskar_type_is_matrix(oskar_vis_header_amp_type(header)) ? 4 : 1; num_blocks = (num_times_total + max_times_per_block - 1) / max_times_per_block; time_start_mjd = oskar_vis_header_time_start_mjd_utc(header) * 86400.0; time_inc_sec = oskar_vis_header_time_inc_sec(header); /* Set visibility meta-data. */ oskar_imager_set_vis_frequency(h, oskar_vis_header_freq_start_hz(header), oskar_vis_header_freq_inc_hz(header), oskar_vis_header_num_channels_total(header)); oskar_imager_set_vis_phase_centre(h, oskar_vis_header_phase_centre_ra_deg(header), oskar_vis_header_phase_centre_dec_deg(header)); /* Create scratch arrays. Weights are all 1. */ uu = oskar_mem_create(coord_prec, OSKAR_CPU, 0, status); vv = oskar_mem_create(coord_prec, OSKAR_CPU, 0, status); ww = oskar_mem_create(coord_prec, OSKAR_CPU, 0, status); time_centroid = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, num_baselines * max_times_per_block, status); time_slice = oskar_mem_create_alias(0, 0, 0, status); weight = oskar_mem_create(h->imager_prec, OSKAR_CPU, num_baselines * num_pols * max_times_per_block, status); oskar_mem_set_value_real(weight, 1.0, 0, 0, status); /* Loop over visibility blocks. */ for (i_block = 0; i_block < num_blocks; ++i_block) { int t, num_times, num_channels, start_time, start_chan, end_chan; int dim_start_and_size[6]; size_t num_rows; if (*status) break; /* Read block metadata. */ oskar_timer_resume(h->tmr_read); oskar_binary_set_query_search_start(vis_file, i_block * tags_per_block, status); oskar_binary_read(vis_file, OSKAR_INT, OSKAR_TAG_GROUP_VIS_BLOCK, OSKAR_VIS_BLOCK_TAG_DIM_START_AND_SIZE, i_block, sizeof(dim_start_and_size), dim_start_and_size, status); start_time = dim_start_and_size[0]; start_chan = dim_start_and_size[1]; num_times = dim_start_and_size[2]; num_channels = dim_start_and_size[3]; num_rows = num_times * num_baselines; end_chan = start_chan + num_channels - 1; /* Fill in the time centroid values. */ for (t = 0; t < num_times; ++t) { oskar_mem_set_alias(time_slice, time_centroid, t * num_baselines, num_baselines, status); oskar_mem_set_value_real(time_slice, time_start_mjd + (start_time + t + 0.5) * time_inc_sec, 0, num_baselines, status); } /* Read the baseline coordinates. */ oskar_binary_read_mem(vis_file, uu, OSKAR_TAG_GROUP_VIS_BLOCK, OSKAR_VIS_BLOCK_TAG_BASELINE_UU, i_block, status); oskar_binary_read_mem(vis_file, vv, OSKAR_TAG_GROUP_VIS_BLOCK, OSKAR_VIS_BLOCK_TAG_BASELINE_VV, i_block, status); oskar_binary_read_mem(vis_file, ww, OSKAR_TAG_GROUP_VIS_BLOCK, OSKAR_VIS_BLOCK_TAG_BASELINE_WW, i_block, status); /* Update the imager with the data. */ oskar_timer_pause(h->tmr_read); oskar_imager_update(h, num_rows, start_chan, end_chan, num_pols, uu, vv, ww, 0, weight, time_centroid, status); *percent_done = (int) round(100.0 * ( (i_block + 1) / (double)(num_blocks * num_files) + i_file / (double)num_files)); if (h->log && percent_next && *percent_done >= *percent_next) { oskar_log_message(h->log, 'S', -2, "%3d%% ...", *percent_done); *percent_next = 10 + 10 * (*percent_done / 10); } } oskar_mem_free(uu, status); oskar_mem_free(vv, status); oskar_mem_free(ww, status); oskar_mem_free(weight, status); oskar_mem_free(time_centroid, status); oskar_mem_free(time_slice, status); oskar_vis_header_free(header, status); oskar_binary_free(vis_file); } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/vis/src/oskar_vis_header_write.c
/* * Copyright (c) 2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "vis/private_vis_header.h" #include "vis/oskar_vis_header.h" #include "binary/oskar_binary.h" #include "mem/oskar_binary_write_mem.h" #include "utility/oskar_binary_write_metadata.h" #include <string.h> #ifdef __cplusplus extern "C" { #endif oskar_Binary* oskar_vis_header_write(const oskar_VisHeader* hdr, const char* filename, int* status) { unsigned char grp = OSKAR_TAG_GROUP_VIS_HEADER; oskar_Binary* h = 0; const char* settings; /* Check if safe to proceed. */ if (*status) return 0; /* Create the handle. */ h = oskar_binary_create(filename, 'w', status); if (*status) { oskar_binary_free(h); return 0; } /* Write the header and common metadata. */ oskar_binary_write_metadata(h, status); /* If settings exist, write out the data. */ settings = oskar_mem_char_const(oskar_vis_header_settings_const(hdr)); if (settings && strlen(settings) > 0) { oskar_binary_write_mem(h, oskar_vis_header_settings_const(hdr), OSKAR_TAG_GROUP_SETTINGS, OSKAR_TAG_SETTINGS, 0, 0, status); } /* Write the telescope model path. */ oskar_binary_write_mem(h, hdr->telescope_path, grp, OSKAR_VIS_HEADER_TAG_TELESCOPE_PATH, 0, 0, status); /* Write the number of binary tags per block. */ oskar_binary_write_int(h, grp, OSKAR_VIS_HEADER_TAG_NUM_TAGS_PER_BLOCK, 0, hdr->num_tags_per_block, status); /* Write dimensions. */ oskar_binary_write_int(h, grp, OSKAR_VIS_HEADER_TAG_WRITE_AUTO_CORRELATIONS, 0, hdr->write_autocorr, status); oskar_binary_write_int(h, grp, OSKAR_VIS_HEADER_TAG_WRITE_CROSS_CORRELATIONS, 0, hdr->write_crosscorr, status); oskar_binary_write_int(h, grp, OSKAR_VIS_HEADER_TAG_AMP_TYPE, 0, hdr->amp_type, status); oskar_binary_write_int(h, grp, OSKAR_VIS_HEADER_TAG_COORD_PRECISION, 0, hdr->coord_precision, status); oskar_binary_write_int(h, grp, OSKAR_VIS_HEADER_TAG_MAX_TIMES_PER_BLOCK, 0, hdr->max_times_per_block, status); oskar_binary_write_int(h, grp, OSKAR_VIS_HEADER_TAG_NUM_TIMES_TOTAL, 0, hdr->num_times_total, status); oskar_binary_write_int(h, grp, OSKAR_VIS_HEADER_TAG_MAX_CHANNELS_PER_BLOCK, 0, hdr->max_channels_per_block, status); oskar_binary_write_int(h, grp, OSKAR_VIS_HEADER_TAG_NUM_CHANNELS_TOTAL, 0, hdr->num_channels_total, status); oskar_binary_write_int(h, grp, OSKAR_VIS_HEADER_TAG_NUM_STATIONS, 0, hdr->num_stations, status); /* Write other visibility metadata. */ oskar_binary_write_int(h, grp, OSKAR_VIS_HEADER_TAG_POL_TYPE, 0, hdr->pol_type, status); oskar_binary_write_int(h, grp, OSKAR_VIS_HEADER_TAG_PHASE_CENTRE_COORD_TYPE, 0, hdr->phase_centre_type, status); oskar_binary_write(h, OSKAR_DOUBLE, grp, OSKAR_VIS_HEADER_TAG_PHASE_CENTRE_DEG, 0, 2 * sizeof(double), hdr->phase_centre_deg, status); oskar_binary_write_double(h, grp, OSKAR_VIS_HEADER_TAG_FREQ_START_HZ, 0, hdr->freq_start_hz, status); oskar_binary_write_double(h, grp, OSKAR_VIS_HEADER_TAG_FREQ_INC_HZ, 0, hdr->freq_inc_hz, status); oskar_binary_write_double(h, grp, OSKAR_VIS_HEADER_TAG_CHANNEL_BANDWIDTH_HZ, 0, hdr->channel_bandwidth_hz, status); oskar_binary_write_double(h, grp, OSKAR_VIS_HEADER_TAG_TIME_START_MJD_UTC, 0, hdr->time_start_mjd_utc, status); oskar_binary_write_double(h, grp, OSKAR_VIS_HEADER_TAG_TIME_INC_SEC, 0, hdr->time_inc_sec, status); oskar_binary_write_double(h, grp, OSKAR_VIS_HEADER_TAG_TIME_AVERAGE_SEC, 0, hdr->time_average_sec, status); oskar_binary_write_double(h, grp, OSKAR_VIS_HEADER_TAG_TELESCOPE_REF_LON_DEG, 0, hdr->telescope_centre_lon_deg, status); oskar_binary_write_double(h, grp, OSKAR_VIS_HEADER_TAG_TELESCOPE_REF_LAT_DEG, 0, hdr->telescope_centre_lat_deg, status); oskar_binary_write_double(h, grp, OSKAR_VIS_HEADER_TAG_TELESCOPE_REF_ALT_M, 0, hdr->telescope_centre_alt_m, status); /* Write the station coordinates. */ oskar_binary_write_mem(h, hdr->station_x_offset_ecef_metres, grp, OSKAR_VIS_HEADER_TAG_STATION_X_OFFSET_ECEF, 0, 0, status); oskar_binary_write_mem(h, hdr->station_y_offset_ecef_metres, grp, OSKAR_VIS_HEADER_TAG_STATION_Y_OFFSET_ECEF, 0, 0, status); oskar_binary_write_mem(h, hdr->station_z_offset_ecef_metres, grp, OSKAR_VIS_HEADER_TAG_STATION_Z_OFFSET_ECEF, 0, 0, status); return h; } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/mem/src/oskar_mem_realloc.c
/* * Copyright (c) 2011-2018, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifdef OSKAR_HAVE_CUDA #include <cuda_runtime_api.h> #endif #include "mem/oskar_mem.h" #include "mem/private_mem.h" #include "utility/oskar_cl_utils.h" #include "utility/oskar_device_utils.h" #include <string.h> #include <stdlib.h> #include <stdio.h> #ifdef __cplusplus extern "C" { #endif void oskar_mem_realloc(oskar_Mem* mem, size_t num_elements, int* status) { size_t element_size, new_size, old_size; /* Check if safe to proceed. */ if (*status) return; /* Check if the structure owns the memory it points to. */ if (mem->owner == 0) { *status = OSKAR_ERR_MEMORY_NOT_ALLOCATED; return; } /* Get size of new and old memory blocks. */ element_size = oskar_mem_element_size(mem->type); if (element_size == 0) { *status = OSKAR_ERR_BAD_DATA_TYPE; return; } new_size = num_elements * element_size; old_size = mem->num_elements * element_size; /* Do nothing if new size and old size are the same. */ if (new_size == old_size) return; /* Check memory location. */ if (mem->location == OSKAR_CPU) { /* Reallocate the memory. */ void* mem_new = NULL; mem_new = realloc(mem->data, new_size); if (!mem_new && (new_size > 0)) { *status = OSKAR_ERR_MEMORY_ALLOC_FAILURE; return; } /* Initialise the new memory if it's larger than the old block. */ if (new_size > old_size) memset((char*)mem_new + old_size, 0, new_size - old_size); /* Set the new meta-data. */ mem->data = (new_size > 0) ? mem_new : 0; mem->num_elements = num_elements; } else if (mem->location == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA /* Allocate and initialise a new block of memory. */ int cuda_error = 0; void* mem_new = NULL; if (new_size > 0) { cuda_error = cudaMalloc(&mem_new, new_size); if (cuda_error) { *status = cuda_error; return; } if (!mem_new) { *status = OSKAR_ERR_MEMORY_ALLOC_FAILURE; return; } } /* Copy contents of old block to new block. */ const size_t copy_size = (old_size > new_size) ? new_size : old_size; if (copy_size > 0) { cuda_error = cudaMemcpy(mem_new, mem->data, copy_size, cudaMemcpyDeviceToDevice); if (cuda_error) *status = cuda_error; } /* Free the old block. */ cudaFree(mem->data); oskar_device_check_error(status); /* Set the new meta-data. */ mem->data = mem_new; mem->num_elements = num_elements; #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } else if (mem->location & OSKAR_CL) { #ifdef OSKAR_HAVE_OPENCL /* Allocate and initialise a new block of memory. */ cl_event event; cl_int error = 0; cl_mem mem_new; mem_new = clCreateBuffer(oskar_cl_context(), CL_MEM_READ_WRITE, new_size, NULL, &error); if (error != CL_SUCCESS) { *status = OSKAR_ERR_MEMORY_ALLOC_FAILURE; return; } /* Copy contents of old block to new block. */ const size_t copy_size = (old_size > new_size) ? new_size : old_size; if (copy_size > 0) { error = clEnqueueCopyBuffer(oskar_cl_command_queue(), mem->buffer, mem_new, 0, 0, copy_size, 0, NULL, &event); if (error != CL_SUCCESS) *status = OSKAR_ERR_MEMORY_COPY_FAILURE; } /* Free the old buffer. */ if (mem->buffer) clReleaseMemObject(mem->buffer); /* Set the new meta-data. */ mem->buffer = mem_new; mem->num_elements = num_elements; #else *status = OSKAR_ERR_OPENCL_NOT_AVAILABLE; #endif } else { *status = OSKAR_ERR_BAD_LOCATION; } } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/telescope/station/src/oskar_evaluate_element_weights.c
/* * Copyright (c) 2012-2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "telescope/station/oskar_evaluate_element_weights.h" #include "telescope/station/oskar_evaluate_element_weights_dft.h" #include "telescope/station/oskar_evaluate_element_weights_errors.h" #ifdef __cplusplus extern "C" { #endif void oskar_evaluate_element_weights(oskar_Mem* weights, oskar_Mem* weights_error, double wavenumber, const oskar_Station* station, double x_beam, double y_beam, double z_beam, int time_index, int* status) { int num_elements; /* Check if safe to proceed. */ if (*status) return; /* Resize weights and weights error work arrays if required. */ num_elements = oskar_station_num_elements(station); if ((int)oskar_mem_length(weights) < num_elements) oskar_mem_realloc(weights, num_elements, status); if ((int)oskar_mem_length(weights_error) < num_elements) oskar_mem_realloc(weights_error, num_elements, status); /* Generate DFT weights. */ oskar_evaluate_element_weights_dft(num_elements, oskar_station_element_measured_x_enu_metres_const(station), oskar_station_element_measured_y_enu_metres_const(station), oskar_station_element_measured_z_enu_metres_const(station), wavenumber, x_beam, y_beam, z_beam, weights, status); /* Apply time-variable errors. */ if (oskar_station_apply_element_errors(station)) { /* Generate weights errors. */ oskar_evaluate_element_weights_errors(num_elements, oskar_station_element_gain_const(station), oskar_station_element_gain_error_const(station), oskar_station_element_phase_offset_rad_const(station), oskar_station_element_phase_error_rad_const(station), oskar_station_seed_time_variable_errors(station), time_index, oskar_station_unique_id(station), weights_error, status); /* Modify the weights (complex multiply with error vector). */ oskar_mem_multiply(0, weights, weights_error, num_elements, status); } /* Modify the weights using the provided apodisation values. */ if (oskar_station_apply_element_weight(station)) { oskar_mem_multiply(0, weights, oskar_station_element_weight_const(station), num_elements, status); } } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/utility/src/oskar_cuda_mem_log.c
/* * Copyright (c) 2013-2016, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "utility/oskar_device_utils.h" #include "utility/oskar_cuda_mem_log.h" #include "log/oskar_log.h" #include <stdlib.h> #ifdef __cplusplus extern "C" { #endif void oskar_cuda_mem_log(oskar_Log* log, int depth, int device_id) { char* name = 0; size_t mem_free = 0, mem_total = 0; /* Record GPU memory usage. */ oskar_device_mem_info(&mem_free, &mem_total); name = oskar_device_name(device_id); oskar_log_message(log, 'M', depth, "Memory on GPU %d [%s] is %.1f%% " "(%.1f/%.1f GB) used.", device_id, name, 100.0 * (1.0 - (mem_free / (double)mem_total)), (mem_total-mem_free)/(1024.*1024.*1024.), mem_total/(1024.*1024.*1024.)); free(name); } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/mem/src/oskar_mem_copy_contents.c
/* * Copyright (c) 2011-2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifdef OSKAR_HAVE_CUDA #include <cuda_runtime_api.h> #endif #include "mem/oskar_mem.h" #include "mem/private_mem.h" #include "utility/oskar_cl_utils.h" #include "utility/oskar_device_utils.h" #include <string.h> #ifdef __cplusplus extern "C" { #endif void oskar_mem_copy_contents(oskar_Mem* dst, const oskar_Mem* src, size_t offset_dst, size_t offset_src, size_t num_elements, int* status) { int location_src, location_dst; size_t bytes, element_size, start_dst, start_src; void *destination; const void *source; /* Check if safe to proceed. */ if (*status) return; /* Return immediately if there is nothing to copy. */ if (src->num_elements == 0 || num_elements == 0) return; /* Check the data types. */ if (src->type != dst->type) { *status = OSKAR_ERR_TYPE_MISMATCH; return; } /* Check the data dimensions. */ if (num_elements > src->num_elements || num_elements > (dst->num_elements - offset_dst)) { *status = OSKAR_ERR_OUT_OF_RANGE; return; } /* Get the number of bytes to copy. */ element_size = oskar_mem_element_size(src->type); bytes = element_size * num_elements; start_dst = element_size * offset_dst; start_src = element_size * offset_src; destination = (void*)((char*)(dst->data) + start_dst); source = (const void*)((const char*)(src->data) + start_src); location_src = src->location; location_dst = dst->location; /* Host to host. */ if (location_src == OSKAR_CPU && location_dst == OSKAR_CPU) { memcpy(destination, source, bytes); return; } /* Host to CUDA device. */ else if (location_src == OSKAR_CPU && location_dst == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA cudaMemcpy(destination, source, bytes, cudaMemcpyHostToDevice); oskar_device_check_error(status); #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif return; } /* CUDA device to host. */ else if (location_src == OSKAR_GPU && location_dst == OSKAR_CPU) { #ifdef OSKAR_HAVE_CUDA cudaMemcpy(destination, source, bytes, cudaMemcpyDeviceToHost); oskar_device_check_error(status); #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif return; } /* CUDA device to CUDA device. */ else if (location_src == OSKAR_GPU && location_dst == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA cudaMemcpy(destination, source, bytes, cudaMemcpyDeviceToDevice); oskar_device_check_error(status); #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif return; } /* Host to OpenCL device. */ else if (location_src == OSKAR_CPU && (location_dst & OSKAR_CL)) { #ifdef OSKAR_HAVE_OPENCL cl_int error; if (!source) { *status = OSKAR_ERR_MEMORY_NOT_ALLOCATED; return; } error = clEnqueueWriteBuffer(oskar_cl_command_queue(), dst->buffer, CL_TRUE, start_dst, bytes, source, 0, NULL, NULL); if (error != CL_SUCCESS) { size_t available; clGetMemObjectInfo(dst->buffer, CL_MEM_SIZE, sizeof(size_t), &available, NULL); fprintf(stderr, "clEnqueueWriteBuffer() error (%d): " "Copy size: %lu, Offset: %lu, Available: %lu\n", error, (unsigned long) bytes, (unsigned long) start_dst, (unsigned long) available); *status = OSKAR_ERR_MEMORY_COPY_FAILURE; } #else *status = OSKAR_ERR_OPENCL_NOT_AVAILABLE; #endif return; } /* OpenCL device to host. */ else if ((location_src & OSKAR_CL) && location_dst == OSKAR_CPU) { #ifdef OSKAR_HAVE_OPENCL cl_int error; error = clEnqueueReadBuffer(oskar_cl_command_queue(), src->buffer, CL_TRUE, start_src, bytes, destination, 0, NULL, NULL); if (error != CL_SUCCESS) { fprintf(stderr, "clEnqueueReadBuffer() error (%d)\n", error); *status = OSKAR_ERR_MEMORY_COPY_FAILURE; } #else *status = OSKAR_ERR_OPENCL_NOT_AVAILABLE; #endif return; } /* OpenCL device to OpenCL device. */ else if ((location_src & OSKAR_CL) && (location_dst & OSKAR_CL)) { #ifdef OSKAR_HAVE_OPENCL cl_int error; error = clEnqueueCopyBuffer(oskar_cl_command_queue(), src->buffer, dst->buffer, start_src, start_dst, bytes, 0, NULL, NULL); if (error != CL_SUCCESS) { fprintf(stderr, "clEnqueueCopyBuffer() error (%d)\n", error); *status = OSKAR_ERR_MEMORY_COPY_FAILURE; } #else *status = OSKAR_ERR_OPENCL_NOT_AVAILABLE; #endif return; } *status = OSKAR_ERR_BAD_LOCATION; } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/convert/oskar_convert_station_uvw_to_baseline_uvw_cuda.h
/* * Copyright (c) 2013-2014, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_CONVERT_STATION_UVW_TO_BASELINE_UVW_CUDA_H_ #define OSKAR_CONVERT_STATION_UVW_TO_BASELINE_UVW_CUDA_H_ /** * @file oskar_convert_station_uvw_to_baseline_uvw_cuda.h */ #include <oskar_global.h> #ifdef __cplusplus extern "C" { #endif /** * @brief * CUDA kernel wrapper to compute baseline coordinates for all station pairs * (single precision). * * @details * Given the (u,v,w) coordinates for each station, this CUDA function computes * the baseline coordinates for all station pairs. * * The output arrays must be pre-sized to length N * (N - 1) / 2, where N is * the number of stations. * * @param[in] num_stations The number of stations. * @param[in] d_u The station u-positions. * @param[in] d_v The station v-positions. * @param[in] d_w The station w-positions. * @param[out] d_uu The baseline u-positions. * @param[out] d_vv The baseline v-positions. * @param[out] d_ww The baseline w-positions. */ OSKAR_EXPORT void oskar_convert_station_uvw_to_baseline_uvw_cuda_f(int num_stations, const float* d_u, const float* d_v, const float* d_w, float* d_uu, float* d_vv, float* d_ww); /** * @brief * CUDA kernel wrapper to compute baseline coordinates for all station pairs * (double precision). * * @details * Given the (u,v,w) coordinates for each station, this CUDA function computes * the baseline coordinates for all station pairs. * * The output arrays must be pre-sized to length N * (N - 1) / 2, where N is * the number of stations. * * @param[in] num_stations The number of stations. * @param[in] d_u The station u-positions. * @param[in] d_v The station v-positions. * @param[in] d_w The station w-positions. * @param[out] d_uu The baseline u-positions. * @param[out] d_vv The baseline v-positions. * @param[out] d_ww The baseline w-positions. */ OSKAR_EXPORT void oskar_convert_station_uvw_to_baseline_uvw_cuda_d(int num_stations, const double* d_u, const double* d_v, const double* d_w, double* d_uu, double* d_vv, double* d_ww); #ifdef __CUDACC__ /* Kernels. */ __global__ void oskar_convert_station_uvw_to_baseline_uvw_cudak_f(int num_stations, const float* u, const float* v, const float* w, float* uu, float* vv, float* ww); __global__ void oskar_convert_station_uvw_to_baseline_uvw_cudak_d(int num_stations, const double* u, const double* v, const double* w, double* uu, double* vv, double* ww); #endif /* __CUDACC__ */ #ifdef __cplusplus } #endif #endif /* OSKAR_CONVERT_STATION_UVW_TO_BASELINE_UVW_CUDA_H_ */
davepallot/OSKAR
oskar/sky/oskar_sky_copy_source_data_cuda.h
/* * Copyright (c) 2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_SKY_COPY_SOURCE_DATA_CUDA_H_ #define OSKAR_SKY_COPY_SOURCE_DATA_CUDA_H_ /** * @file oskar_sky_copy_source_data_cuda.h */ #include <oskar_global.h> #ifdef __cplusplus extern "C" { #endif /** * @brief * Copies source data to new arrays based on mask values (single precision). * * @details * Copies source data to new arrays wherever the input mask value is true. * * Each of the output arrays must be sized large enough to hold all the input * data if necessary. * * Note that all arrays must be in device memory. * * @param[in] num Number of input sources. * @param[out] num_out Number of sources copied. * @param[in] mask Input mask values. * @param[in] indices Indices into output array from prefix sum. * @param[in] ra_in Input right ascension coordinates. * @param[out] ra_out Output right ascension coordinates. * @param[in] dec_in Input declination coordinates. * @param[out] dec_out Output declination coordinates. * @param[in] I_in Input Stokes I values. * @param[out] I_out Output Stokes I values. * @param[in] Q_in Input Stokes Q values. * @param[out] Q_out Output Stokes Q values. * @param[in] U_in Input Stokes U values. * @param[out] U_out Output Stokes U values. * @param[in] V_in Input Stokes V values. * @param[out] V_out Output Stokes V values. * @param[in] ref_in Input reference frequency values. * @param[out] ref_out Output reference frequency values. * @param[in] sp_in Input spectral index values. * @param[out] sp_out Output spectral index values. * @param[in] rm_in Input rotation measure values. * @param[out] rm_out Output rotation measure values. * @param[in] l_in Input l-direction-cosine values. * @param[out] l_out Output l-direction-cosine values. * @param[in] m_in Input m-direction-cosine values. * @param[out] m_out Output m-direction-cosine values. * @param[in] n_in Input n-direction-cosine values. * @param[out] n_out Output n-direction-cosine values. * @param[in] a_in Input Gaussian parameter a values. * @param[out] a_out Output Gaussian parameter a values. * @param[in] b_in Input Gaussian parameter b values. * @param[out] b_out Output Gaussian parameter b values. * @param[in] c_in Input Gaussian parameter c values. * @param[out] c_out Output Gaussian parameter c values. * @param[in] maj_in Input Gaussian major axis values. * @param[out] maj_out Output Gaussian major axis values. * @param[in] min_in Input Gaussian minor axis values. * @param[out] min_out Output Gaussian minor axis values. * @param[in] pa_in Input Gaussian position angle values. * @param[out] pa_out Output Gaussian position angle values. */ OSKAR_EXPORT void oskar_sky_copy_source_data_cuda_f(int num, int* num_out, const int* mask, const int* indices, const float* ra_in, float* ra_out, const float* dec_in, float* dec_out, const float* I_in, float* I_out, const float* Q_in, float* Q_out, const float* U_in, float* U_out, const float* V_in, float* V_out, const float* ref_in, float* ref_out, const float* sp_in, float* sp_out, const float* rm_in, float* rm_out, const float* l_in, float* l_out, const float* m_in, float* m_out, const float* n_in, float* n_out, const float* a_in, float* a_out, const float* b_in, float* b_out, const float* c_in, float* c_out, const float* maj_in, float* maj_out, const float* min_in, float* min_out, const float* pa_in, float* pa_out ); /** * @brief * Copies source data to new arrays based on mask values (double precision). * * @details * Copies source data to new arrays wherever the input mask value is true. * * Each of the output arrays must be sized large enough to hold all the input * data if necessary. * * Note that all arrays must be in device memory. * * @param[in] num Number of input sources. * @param[out] num_out Number of sources copied. * @param[in] mask Input mask values. * @param[in] indices Indices into output array from prefix sum. * @param[in] ra_in Input right ascension coordinates. * @param[out] ra_out Output right ascension coordinates. * @param[in] dec_in Input declination coordinates. * @param[out] dec_out Output declination coordinates. * @param[in] I_in Input Stokes I values. * @param[out] I_out Output Stokes I values. * @param[in] Q_in Input Stokes Q values. * @param[out] Q_out Output Stokes Q values. * @param[in] U_in Input Stokes U values. * @param[out] U_out Output Stokes U values. * @param[in] V_in Input Stokes V values. * @param[out] V_out Output Stokes V values. * @param[in] ref_in Input reference frequency values. * @param[out] ref_out Output reference frequency values. * @param[in] sp_in Input spectral index values. * @param[out] sp_out Output spectral index values. * @param[in] rm_in Input rotation measure values. * @param[out] rm_out Output rotation measure values. * @param[in] l_in Input l-direction-cosine values. * @param[out] l_out Output l-direction-cosine values. * @param[in] m_in Input m-direction-cosine values. * @param[out] m_out Output m-direction-cosine values. * @param[in] n_in Input n-direction-cosine values. * @param[out] n_out Output n-direction-cosine values. * @param[in] a_in Input Gaussian parameter a values. * @param[out] a_out Output Gaussian parameter a values. * @param[in] b_in Input Gaussian parameter b values. * @param[out] b_out Output Gaussian parameter b values. * @param[in] c_in Input Gaussian parameter c values. * @param[out] c_out Output Gaussian parameter c values. * @param[in] maj_in Input Gaussian major axis values. * @param[out] maj_out Output Gaussian major axis values. * @param[in] min_in Input Gaussian minor axis values. * @param[out] min_out Output Gaussian minor axis values. * @param[in] pa_in Input Gaussian position angle values. * @param[out] pa_out Output Gaussian position angle values. */ OSKAR_EXPORT void oskar_sky_copy_source_data_cuda_d(int num, int* num_out, const int* mask, const int* indices, const double* ra_in, double* ra_out, const double* dec_in, double* dec_out, const double* I_in, double* I_out, const double* Q_in, double* Q_out, const double* U_in, double* U_out, const double* V_in, double* V_out, const double* ref_in, double* ref_out, const double* sp_in, double* sp_out, const double* rm_in, double* rm_out, const double* l_in, double* l_out, const double* m_in, double* m_out, const double* n_in, double* n_out, const double* a_in, double* a_out, const double* b_in, double* b_out, const double* c_in, double* c_out, const double* maj_in, double* maj_out, const double* min_in, double* min_out, const double* pa_in, double* pa_out ); #ifdef __cplusplus } #endif #endif /* OSKAR_SKY_COPY_SOURCE_DATA_CUDA_H_ */
davepallot/OSKAR
oskar/mem/oskar_mem_random_gaussian_cuda.h
<gh_stars>1-10 /* * Copyright (c) 2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_MEM_RANDOM_GAUSSIAN_CUDA_H_ #define OSKAR_MEM_RANDOM_GAUSSIAN_CUDA_H_ /** * @file oskar_mem_random_gaussian_cuda.h */ #include <oskar_global.h> #ifdef __cplusplus extern "C" { #endif /** * @brief * Generates random numbers from a normalised Gaussian distribution * (single precision, CUDA version). * * @details * Generates random numbers selected from a Gaussian distribution * with a mean of zero and standard deviation of 1. * * This function uses the counter-based stateless "Philox" random number * generator from the Random123 library for high performance * and crush-resistance. * * See <NAME> et al. "Parallel random numbers: as easy as 1, 2, 3" * (doi:10.1145/2063384.2063405) * * Together, the seed and the counter values completely define the * generated data. It is the caller's responsibility to increment the counter * value(s) between calls. * * @param[in] num_elements Number of elements to fill. * @param[in,out] d_data Pointer to memory block to fill (on device). * @param[in] seed Random seed. * @param[in] counter1 User-defined counter. * @param[in] counter2 User-defined counter. * @param[in] counter3 User-defined counter. * @param[in] std Standard deviation of distribution. */ OSKAR_EXPORT void oskar_mem_random_gaussian_cuda_f(int num_elements, float* d_data, unsigned int seed, unsigned int counter1, unsigned int counter2, unsigned int counter3, float std); /** * @brief * Generates random numbers from a normalised Gaussian distribution * (double precision, CPU version). * * @details * Generates random numbers selected from a Gaussian distribution * with a mean of zero and standard deviation of 1. * * This function uses the counter-based stateless "Philox" random number * generator from the Random123 library for high performance * and crush-resistance. * * See John K. Salmon et al. "Parallel random numbers: as easy as 1, 2, 3" * (doi:10.1145/2063384.2063405) * * Together, the seed and the counter values completely define the * generated data. It is the caller's responsibility to increment the counter * value(s) between calls. * * @param[in] num_elements Number of elements to fill. * @param[in,out] d_data Pointer to memory block to fill (on device). * @param[in] seed Random seed. * @param[in] counter1 User-defined counter. * @param[in] counter2 User-defined counter. * @param[in] counter3 User-defined counter. * @param[in] std Standard deviation of distribution. */ OSKAR_EXPORT void oskar_mem_random_gaussian_cuda_d(int num_elements, double* d_data, unsigned int seed, unsigned int counter1, unsigned int counter2, unsigned int counter3, double std); #ifdef __cplusplus } #endif #endif /* OSKAR_MEM_RANDOM_GAUSSIAN_CUDA_H_ */
davepallot/OSKAR
oskar/imager/private_imager.h
/* * Copyright (c) 2016-2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifdef OSKAR_HAVE_CUDA #include <cufft.h> #endif #include <fitsio.h> #include <mem/oskar_mem.h> #include <log/oskar_log.h> #include <utility/oskar_thread.h> #include <utility/oskar_timer.h> #ifdef __cplusplus extern "C" { #endif /* Memory allocated per GPU. */ struct DeviceData { oskar_Mem *uu, *vv, *ww, *amp, *weight, *l, *m, *n; oskar_Mem *block_dev, *block_cpu; }; typedef struct DeviceData DeviceData; struct oskar_Imager { char* output_name[4]; fitsfile* fits_file[4]; oskar_Log* log; oskar_Timer *tmr_grid_update, *tmr_grid_finalise, *tmr_init; oskar_Timer *tmr_read, *tmr_write; /* Settings parameters. */ int imager_prec, num_devices, num_gpus, *gpu_ids, fft_on_gpu; int chan_snaps, im_type, num_im_channels, num_im_pols, pol_offset; int algorithm, image_size, use_stokes, support, oversample; int generate_w_kernels_on_gpu, set_cellsize, set_fov, weighting; int num_files, scale_norm_with_num_input_files; char direction_type, kernel_type; char **input_files, *input_root, *output_root, *ms_column; double cellsize_rad, fov_deg, image_padding, im_centre_deg[2]; double uv_filter_min, uv_filter_max; double time_min_utc, time_max_utc, freq_min_hz, freq_max_hz; /* Visibility meta-data. */ int num_sel_freqs; double *im_freqs, *sel_freqs; double vis_freq_start_hz, freq_inc_hz; /* State. */ int status, i_block; oskar_Mutex* mutex; /* Scratch data. */ oskar_Mem *uu_im, *vv_im, *ww_im, *vis_im, *weight_im, *time_im; oskar_Mem *uu_tmp, *vv_tmp, *ww_tmp, *stokes, *weight_tmp; int coords_only; /* Set if doing a first pass for uniform weighting. */ int num_planes; /* For each output channel and polarisation. */ double *plane_norm, delta_l, delta_m, delta_n, M[9]; oskar_Mem **planes, **weights_grids; /* DFT imager data. */ oskar_Mem *l, *m, *n; /* FFT imager data. */ int grid_size; oskar_Mem *conv_func, *corr_func, *fftpack_wsave, *fftpack_work; #ifdef OSKAR_HAVE_CUDA cufftHandle cufft_plan; #endif /* W-projection imager data. */ size_t ww_points; int num_w_planes, conv_size_half; double w_scale, ww_min, ww_max, ww_rms; oskar_Mem *w_kernels, *w_support, *w_kernels_compact, *w_kernel_start; /* Memory allocated per GPU (array of DeviceData structures). */ DeviceData* d; }; #ifndef OSKAR_IMAGER_TYPEDEF_ #define OSKAR_IMAGER_TYPEDEF_ typedef struct oskar_Imager oskar_Imager; #endif /* OSKAR_IMAGER_TYPEDEF_ */ #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/vis/src/oskar_vis_create.c
<reponame>davepallot/OSKAR /* * Copyright (c) 2011-2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "vis/private_vis.h" #include "vis/oskar_vis.h" #ifdef __cplusplus extern "C" { #endif oskar_Vis* oskar_vis_create(int amp_type, int location, int num_channels, int num_times, int num_stations, int* status) { oskar_Vis* vis = 0; int num_baselines, type; size_t num_amps, num_coords; /* Check type. */ if (oskar_type_is_double(amp_type)) type = OSKAR_DOUBLE; else if (oskar_type_is_single(amp_type)) type = OSKAR_SINGLE; else { *status = OSKAR_ERR_BAD_DATA_TYPE; return 0; } if (!oskar_type_is_complex(amp_type)) { *status = OSKAR_ERR_BAD_DATA_TYPE; return 0; } /* Allocate the structure. */ vis = (oskar_Vis*) malloc(sizeof(oskar_Vis)); /* Set dimensions. */ num_baselines = num_stations * (num_stations - 1) / 2; vis->num_stations = num_stations; vis->num_channels = num_channels; vis->num_times = num_times; vis->num_baselines = num_baselines; num_coords = (size_t) num_baselines; num_coords *= num_times; num_amps = num_coords; num_amps *= num_channels; /* Initialise meta-data. */ vis->freq_start_hz = 0.0; vis->freq_inc_hz = 0.0; vis->channel_bandwidth_hz = 0.0; vis->time_start_mjd_utc = 0.0; vis->time_inc_sec = 0.0; vis->time_average_sec = 0.0; vis->phase_centre_ra_deg = 0.0; vis->phase_centre_dec_deg = 0.0; vis->telescope_lon_deg = 0.0; vis->telescope_lat_deg = 0.0; vis->telescope_alt_metres = 0.0; /* Initialise memory. */ vis->settings_path = oskar_mem_create(OSKAR_CHAR, OSKAR_CPU, 0, status); vis->telescope_path = oskar_mem_create(OSKAR_CHAR, OSKAR_CPU, 0, status); vis->settings = oskar_mem_create(OSKAR_CHAR, OSKAR_CPU, 0, status); vis->station_x_offset_ecef_metres = oskar_mem_create(type, location, num_stations, status); vis->station_y_offset_ecef_metres = oskar_mem_create(type, location, num_stations, status); vis->station_z_offset_ecef_metres = oskar_mem_create(type, location, num_stations, status); vis->baseline_uu_metres = oskar_mem_create(type, location, num_coords, status); vis->baseline_vv_metres = oskar_mem_create(type, location, num_coords, status); vis->baseline_ww_metres = oskar_mem_create(type, location, num_coords, status); vis->amplitude = oskar_mem_create(amp_type, location, num_amps, status); /* Return handle to structure. */ return vis; } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/imager/src/oskar_imager_update.c
/* * Copyright (c) 2016-2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "imager/private_imager.h" #include "convert/oskar_convert_ecef_to_baseline_uvw.h" #include "imager/oskar_grid_weights.h" #include "imager/oskar_imager.h" #include "imager/private_imager_create_fits_files.h" #include "imager/private_imager_filter_time.h" #include "imager/private_imager_filter_uv.h" #include "imager/private_imager_set_num_planes.h" #include "imager/private_imager_select_data.h" #include "imager/private_imager_update_plane_dft.h" #include "imager/private_imager_update_plane_fft.h" #include "imager/private_imager_update_plane_wproj.h" #include "imager/private_imager_weight_radial.h" #include "imager/private_imager_weight_uniform.h" #include <math.h> #include <stdlib.h> #include <stdio.h> #ifdef __cplusplus extern "C" { #endif static void oskar_imager_allocate_planes(oskar_Imager* h, int *status); static void oskar_imager_update_weights_grid(oskar_Imager* h, size_t num_points, const oskar_Mem* uu, const oskar_Mem* vv, const oskar_Mem* ww, const oskar_Mem* weight, oskar_Mem* weights_grid, int* status); void oskar_imager_update_from_block(oskar_Imager* h, const oskar_VisHeader* header, const oskar_VisBlock* block, int* status) { int t, start_time, start_chan, end_chan; int num_baselines, num_channels, num_pols, num_times; size_t num_rows; double time_start_mjd, time_inc_sec; oskar_Mem *weight = 0, *weight_ptr = 0, *time_centroid, *time_slice; oskar_Mem *scratch = 0; const oskar_Mem* ptr; if (*status) return; /* Check that cross-correlations exist. */ if (!oskar_vis_block_has_cross_correlations(block)) return; /* Get dimensions from the block. */ start_time = oskar_vis_block_start_time_index(block); start_chan = oskar_vis_block_start_channel_index(block); num_baselines = oskar_vis_block_num_baselines(block); num_channels = oskar_vis_block_num_channels(block); num_pols = oskar_vis_block_num_pols(block); num_times = oskar_vis_block_num_times(block); num_rows = num_baselines * num_times; end_chan = start_chan + num_channels - 1; /* Get visibility meta-data. */ time_start_mjd = oskar_vis_header_time_start_mjd_utc(header) * 86400.0; time_inc_sec = oskar_vis_header_time_inc_sec(header); oskar_imager_set_vis_frequency(h, oskar_vis_header_freq_start_hz(header), oskar_vis_header_freq_inc_hz(header), oskar_vis_header_num_channels_total(header)); oskar_imager_set_vis_phase_centre(h, oskar_vis_header_phase_centre_ra_deg(header), oskar_vis_header_phase_centre_dec_deg(header)); /* Create scratch arrays. Weights are all 1. */ if (num_channels > 1) scratch = oskar_mem_create(oskar_mem_type( oskar_vis_block_cross_correlations_const(block)), OSKAR_CPU, num_rows * num_channels, status); if (!weight) { size_t weight_len = num_rows * num_pols; weight = oskar_mem_create(oskar_mem_precision( oskar_vis_block_cross_correlations_const(block)), OSKAR_CPU, weight_len, status); oskar_mem_set_value_real(weight, 1.0, 0, weight_len, status); weight_ptr = weight; } /* Fill in the time centroid values. */ time_centroid = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, num_rows, status); time_slice = oskar_mem_create_alias(0, 0, 0, status); for (t = 0; t < num_times; ++t) { oskar_mem_set_alias(time_slice, time_centroid, t * num_baselines, num_baselines, status); oskar_mem_set_value_real(time_slice, time_start_mjd + (start_time + t + 0.5) * time_inc_sec, 0, num_baselines, status); } /* Swap baseline and channel dimensions. */ ptr = oskar_vis_block_cross_correlations_const(block); #define SWAP_LOOP \ for (t = 0; t < num_times; ++t) \ for (c = 0; c < num_channels; ++c) \ for (b = 0; b < num_baselines; ++b) \ for (p = 0; p < num_pols; ++p) \ { \ k = (num_pols * (num_baselines * \ (num_channels * t + c) + b) + p) << 1; \ l = (num_pols * (num_channels * \ (num_baselines * t + b) + c) + p) << 1; \ out[l] = in[k]; \ out[l + 1] = in[k + 1]; \ } if (num_channels != 1) { int b, c, p; size_t k, l; if (oskar_mem_precision(ptr) == OSKAR_SINGLE) { const float *in; float *out; in = oskar_mem_float_const(ptr, status); out = oskar_mem_float(scratch, status); SWAP_LOOP } else { const double *in; double *out; in = oskar_mem_double_const(ptr, status); out = oskar_mem_double(scratch, status); SWAP_LOOP } ptr = scratch; } #undef SWAP_LOOP /* Update the imager with the data. */ oskar_imager_update(h, num_rows, start_chan, end_chan, num_pols, oskar_vis_block_baseline_uu_metres_const(block), oskar_vis_block_baseline_vv_metres_const(block), oskar_vis_block_baseline_ww_metres_const(block), ptr, weight_ptr, time_centroid, status); oskar_mem_free(weight, status); oskar_mem_free(scratch, status); oskar_mem_free(time_centroid, status); oskar_mem_free(time_slice, status); } void oskar_imager_update(oskar_Imager* h, size_t num_rows, int start_chan, int end_chan, int num_pols, const oskar_Mem* uu, const oskar_Mem* vv, const oskar_Mem* ww, const oskar_Mem* amps, const oskar_Mem* weight, const oskar_Mem* time_centroid, int* status) { int c, p, plane; size_t max_num_vis; oskar_Mem *tu = 0, *tv = 0, *tw = 0, *ta = 0, *th = 0; const oskar_Mem *u_in, *v_in, *w_in, *amp_in = 0, *weight_in; if (*status) return; /* Set dimensions. */ if (num_rows == 0) num_rows = oskar_mem_length(uu); /* Check polarisation type. */ if (num_pols == 1 && h->im_type != OSKAR_IMAGE_TYPE_I && h->im_type != OSKAR_IMAGE_TYPE_PSF) { *status = OSKAR_ERR_INVALID_ARGUMENT; return; } /* Ensure image/grid planes exist and algorithm has been initialised. */ oskar_imager_set_num_planes(h, status); oskar_imager_check_init(h, status); oskar_imager_allocate_planes(h, status); if (*status) return; /* Convert precision of input data if required. */ u_in = uu; v_in = vv; w_in = ww; weight_in = weight; if (!h->coords_only) { if (!amps) { *status = OSKAR_ERR_MEMORY_NOT_ALLOCATED; return; } amp_in = amps; if (oskar_mem_precision(amps) != h->imager_prec) { ta = oskar_mem_convert_precision(amps, h->imager_prec, status); amp_in = ta; } /* Convert linear polarisations to Stokes parameters if required. */ if (h->use_stokes) { oskar_imager_linear_to_stokes(amp_in, &h->stokes, status); amp_in = h->stokes; } } if (oskar_mem_precision(uu) != h->imager_prec) { tu = oskar_mem_convert_precision(uu, h->imager_prec, status); u_in = tu; } if (oskar_mem_precision(vv) != h->imager_prec) { tv = oskar_mem_convert_precision(vv, h->imager_prec, status); v_in = tv; } if (oskar_mem_precision(ww) != h->imager_prec) { tw = oskar_mem_convert_precision(ww, h->imager_prec, status); w_in = tw; } if (oskar_mem_precision(weight) != h->imager_prec) { th = oskar_mem_convert_precision(weight, h->imager_prec, status); weight_in = th; } /* Ensure work arrays are large enough. */ max_num_vis = num_rows; if (!h->chan_snaps) max_num_vis *= (1 + end_chan - start_chan); oskar_mem_realloc(h->uu_im, max_num_vis, status); oskar_mem_realloc(h->vv_im, max_num_vis, status); oskar_mem_realloc(h->ww_im, max_num_vis, status); oskar_mem_realloc(h->vis_im, max_num_vis, status); oskar_mem_realloc(h->weight_im, max_num_vis, status); if (h->direction_type == 'R') { oskar_mem_realloc(h->uu_tmp, max_num_vis, status); oskar_mem_realloc(h->vv_tmp, max_num_vis, status); oskar_mem_realloc(h->ww_tmp, max_num_vis, status); } /* Loop over each image plane being made. */ for (c = 0; c < h->num_im_channels; ++c) { for (p = 0; p < h->num_im_pols; ++p) { oskar_Mem *pu, *pv, *pw; size_t num_vis = 0; if (*status) break; /* Get all visibility data needed to update this plane. */ pu = h->uu_im; pv = h->vv_im; pw = h->ww_im; if (h->direction_type == 'R') { pu = h->uu_tmp; pv = h->vv_tmp; pw = h->ww_tmp; } oskar_imager_select_data(h, num_rows, start_chan, end_chan, num_pols, u_in, v_in, w_in, amp_in, weight_in, time_centroid, h->im_freqs[c], p, &num_vis, pu, pv, pw, h->vis_im, h->weight_im, h->time_im, status); /* Skip if nothing was selected. */ if (num_vis == 0) continue; /* Rotate baseline coordinates if required. */ if (h->direction_type == 'R') oskar_imager_rotate_coords(h, num_vis, h->uu_tmp, h->vv_tmp, h->ww_tmp, h->uu_im, h->vv_im, h->ww_im); /* Overwrite visibilities if making PSF, or phase rotate. */ if (h->im_type == OSKAR_IMAGE_TYPE_PSF) oskar_mem_set_value_real(h->vis_im, 1.0, 0, 0, status); else if (h->direction_type == 'R' && !h->coords_only) oskar_imager_rotate_vis(h, num_vis, h->uu_tmp, h->vv_tmp, h->ww_tmp, h->vis_im); /* Apply time and baseline length filters if required. */ oskar_imager_filter_time(h, &num_vis, h->uu_im, h->vv_im, h->ww_im, h->vis_im, h->weight_im, h->time_im, status); oskar_imager_filter_uv(h, &num_vis, h->uu_im, h->vv_im, h->ww_im, h->vis_im, h->weight_im, status); /* Update this image plane with the visibilities. */ plane = h->num_im_pols * c + p; if (h->coords_only) oskar_imager_update_plane(h, num_vis, h->uu_im, h->vv_im, h->ww_im, 0, h->weight_im, 0, 0, h->weights_grids[plane], status); else oskar_imager_update_plane(h, num_vis, h->uu_im, h->vv_im, h->ww_im, h->vis_im, h->weight_im, h->planes[plane], &h->plane_norm[plane], h->weights_grids[plane], status); } } oskar_mem_free(tu, status); oskar_mem_free(tv, status); oskar_mem_free(tw, status); oskar_mem_free(ta, status); oskar_mem_free(th, status); } void oskar_imager_update_plane(oskar_Imager* h, size_t num_vis, const oskar_Mem* uu, const oskar_Mem* vv, const oskar_Mem* ww, const oskar_Mem* amps, const oskar_Mem* weight, oskar_Mem* plane, double* plane_norm, oskar_Mem* weights_grid, int* status) { oskar_Mem *tu = 0, *tv = 0, *tw = 0, *ta = 0, *th = 0; const oskar_Mem *pu, *pv, *pw, *pa, *ph; if (*status || num_vis == 0) return; oskar_timer_resume(h->tmr_grid_update); /* Convert precision of input data if required. */ pu = uu; pv = vv; pw = ww; ph = weight; if (oskar_mem_precision(uu) != h->imager_prec) { tu = oskar_mem_convert_precision(uu, h->imager_prec, status); pu = tu; } if (oskar_mem_precision(vv) != h->imager_prec) { tv = oskar_mem_convert_precision(vv, h->imager_prec, status); pv = tv; } if (oskar_mem_precision(ww) != h->imager_prec) { tw = oskar_mem_convert_precision(ww, h->imager_prec, status); pw = tw; } if (oskar_mem_precision(weight) != h->imager_prec) { th = oskar_mem_convert_precision(weight, h->imager_prec, status); ph = th; } /* Just update the grid of weights if we're in coordinate-only mode. */ if (h->coords_only) { oskar_imager_update_weights_grid(h, num_vis, pu, pv, pw, ph, weights_grid, status); } else { size_t num_skipped = 0; /* Convert precision of visibility amplitudes if required. */ pa = amps; if (oskar_mem_precision(amps) != h->imager_prec) { ta = oskar_mem_convert_precision(amps, h->imager_prec, status); pa = ta; } /* Check imager is ready. */ oskar_imager_check_init(h, status); /* Re-weight visibilities if required. */ switch (h->weighting) { case OSKAR_WEIGHTING_NATURAL: /* Nothing to do. */ break; case OSKAR_WEIGHTING_RADIAL: oskar_imager_weight_radial(num_vis, pu, pv, ph, h->weight_tmp, status); ph = h->weight_tmp; break; case OSKAR_WEIGHTING_UNIFORM: oskar_imager_weight_uniform(num_vis, pu, pv, ph, h->weight_tmp, h->cellsize_rad, oskar_imager_plane_size(h), weights_grid, status); ph = h->weight_tmp; break; default: *status = OSKAR_ERR_FUNCTION_NOT_AVAILABLE; break; } /* Update the supplied plane with the supplied visibilities. */ switch (h->algorithm) { case OSKAR_ALGORITHM_DFT_2D: case OSKAR_ALGORITHM_DFT_3D: oskar_imager_update_plane_dft(h, num_vis, pu, pv, pw, pa, ph, plane, plane_norm, status); break; case OSKAR_ALGORITHM_FFT: oskar_imager_update_plane_fft(h, num_vis, pu, pv, pa, ph, plane, plane_norm, &num_skipped, status); break; case OSKAR_ALGORITHM_WPROJ: oskar_imager_update_plane_wproj(h, num_vis, pu, pv, pw, pa, ph, plane, plane_norm, &num_skipped, status); break; default: *status = OSKAR_ERR_FUNCTION_NOT_AVAILABLE; break; } if (num_skipped > 0) printf("WARNING: Skipped %lu visibility points.\n", (unsigned long) num_skipped); } /* Clean up. */ oskar_mem_free(tu, status); oskar_mem_free(tv, status); oskar_mem_free(tw, status); oskar_mem_free(ta, status); oskar_mem_free(th, status); oskar_timer_pause(h->tmr_grid_update); } void oskar_imager_update_weights_grid(oskar_Imager* h, size_t num_points, const oskar_Mem* uu, const oskar_Mem* vv, const oskar_Mem* ww, const oskar_Mem* weight, oskar_Mem* weights_grid, int* status) { if (*status) return; /* Update the weights grid. */ if (h->weighting == OSKAR_WEIGHTING_UNIFORM) { int grid_size; size_t num_cells, num_skipped = 0; /* Resize the grid of weights if needed. */ grid_size = oskar_imager_plane_size(h); num_cells = grid_size * grid_size; if (oskar_mem_length(weights_grid) < num_cells) oskar_mem_realloc(weights_grid, num_cells, status); if (*status) return; if (oskar_mem_precision(weights_grid) == OSKAR_DOUBLE) oskar_grid_weights_write_d(num_points, oskar_mem_double_const(uu, status), oskar_mem_double_const(vv, status), oskar_mem_double_const(weight, status), h->cellsize_rad, grid_size, &num_skipped, oskar_mem_double(weights_grid, status)); else oskar_grid_weights_write_f(num_points, oskar_mem_float_const(uu, status), oskar_mem_float_const(vv, status), oskar_mem_float_const(weight, status), (float) (h->cellsize_rad), grid_size, &num_skipped, oskar_mem_float(weights_grid, status)); if (num_skipped > 0) printf("WARNING: Skipped %lu visibility weights.\n", (unsigned long) num_skipped); } /* Update baseline W minimum, maximum and RMS. */ if (h->algorithm == OSKAR_ALGORITHM_WPROJ) { size_t j; double val; if (oskar_mem_precision(ww) == OSKAR_DOUBLE) { const double *p = oskar_mem_double_const(ww, status); for (j = 0; j < num_points; ++j) { val = fabs(p[j]); h->ww_rms += (val * val); if (val < h->ww_min) h->ww_min = val; if (val > h->ww_max) h->ww_max = val; } } else { const float *p = oskar_mem_float_const(ww, status); for (j = 0; j < num_points; ++j) { val = fabs((double) (p[j])); h->ww_rms += (val * val); if (val < h->ww_min) h->ww_min = val; if (val > h->ww_max) h->ww_max = val; } } h->ww_points += num_points; } } void oskar_imager_allocate_planes(oskar_Imager* h, int *status) { int i, plane_size; if (*status) return; /* Allocate empty weights grids if required. */ if (!h->weights_grids) { h->weights_grids = (oskar_Mem**) calloc(h->num_planes, sizeof(oskar_Mem*)); for (i = 0; i < h->num_planes; ++i) h->weights_grids[i] = oskar_mem_create(h->imager_prec, OSKAR_CPU, 0, status); } /* If we're in coordinate-only mode, or the planes already exist, * there's nothing more to do here. */ if (h->coords_only || h->planes) return; /* Allocate the image or visibility planes. */ h->planes = (oskar_Mem**) calloc(h->num_planes, sizeof(oskar_Mem*)); h->plane_norm = (double*) calloc(h->num_planes, sizeof(double)); plane_size = oskar_imager_plane_size(h); for (i = 0; i < h->num_planes; ++i) h->planes[i] = oskar_mem_create(oskar_imager_plane_type(h), OSKAR_CPU, plane_size * plane_size, status); /* Create FITS files for the planes if required. */ oskar_imager_create_fits_files(h, status); } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/mem/src/oskar_mem_save_ascii.c
<gh_stars>1-10 /* * Copyright (c) 2013-2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "mem/oskar_mem.h" #include "mem/private_mem.h" #include <stdarg.h> #ifdef __cplusplus extern "C" { #endif #define SDF "% .6e " #define SDD "% .14e " void oskar_mem_save_ascii(FILE* file, size_t num_mem, size_t num_elements, int* status, ...) { int type; size_t i, j; va_list args; oskar_Mem** handles; /* Array of oskar_Mem pointers in CPU memory. */ /* Check if safe to proceed. */ if (*status) return; /* Check there are at least the number of specified elements in * each array. */ va_start(args, status); for (i = 0; i < num_mem; ++i) { const oskar_Mem* mem; mem = va_arg(args, const oskar_Mem*); if (oskar_mem_length(mem) < num_elements) *status = OSKAR_ERR_DIMENSION_MISMATCH; } va_end(args); /* Check if safe to proceed. */ if (*status) return; /* Allocate and set up the handle array. */ handles = (oskar_Mem**) malloc(num_mem * sizeof(oskar_Mem*)); va_start(args, status); for (i = 0; i < num_mem; ++i) { oskar_Mem* mem; mem = va_arg(args, oskar_Mem*); if (oskar_mem_location(mem) != OSKAR_CPU) { handles[i] = oskar_mem_create_copy(mem, OSKAR_CPU, status); } else { handles[i] = mem; } } va_end(args); for (j = 0; j < num_elements; ++j) { /* Break if error. */ if (*status) break; for (i = 0; i < num_mem; ++i) { const void* data; data = oskar_mem_void_const(handles[i]); type = oskar_mem_type(handles[i]); switch (type) { case OSKAR_SINGLE: { fprintf(file, SDF, ((const float*)data)[j]); continue; } case OSKAR_DOUBLE: { fprintf(file, SDD, ((const double*)data)[j]); continue; } case OSKAR_SINGLE_COMPLEX: { float2 d; d = ((const float2*)data)[j]; fprintf(file, SDF SDF, d.x, d.y); continue; } case OSKAR_DOUBLE_COMPLEX: { double2 d; d = ((const double2*)data)[j]; fprintf(file, SDD SDD, d.x, d.y); continue; } case OSKAR_SINGLE_COMPLEX_MATRIX: { float4c d; d = ((const float4c*)data)[j]; fprintf(file, SDF SDF SDF SDF SDF SDF SDF SDF, d.a.x, d.a.y, d.b.x, d.b.y, d.c.x, d.c.y, d.d.x, d.d.y); continue; } case OSKAR_DOUBLE_COMPLEX_MATRIX: { double4c d; d = ((const double4c*)data)[j]; fprintf(file, SDD SDD SDD SDD SDD SDD SDD SDD, d.a.x, d.a.y, d.b.x, d.b.y, d.c.x, d.c.y, d.d.x, d.d.y); continue; } case OSKAR_CHAR: { putc(((const char*)data)[j], file); continue; } case OSKAR_INT: { fprintf(file, "%5d ", ((const int*)data)[j]); continue; } default: { *status = OSKAR_ERR_BAD_DATA_TYPE; continue; } } } putc('\n', file); } /* Free any temporary memory used by this function. */ va_start(args, status); for (i = 0; i < num_mem; ++i) { const oskar_Mem* mem; mem = va_arg(args, const oskar_Mem*); if (oskar_mem_location(mem) != OSKAR_CPU) { oskar_mem_free(handles[i], status); } } va_end(args); /* Free the handle array. */ free(handles); } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/utility/oskar_vector_types.h
/* * Copyright (c) 2011-2014, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_VECTOR_TYPES_H_ #define OSKAR_VECTOR_TYPES_H_ /** * @file oskar_vector_types.h */ #ifdef __cplusplus extern "C" { #endif #ifdef __CUDACC__ /* Include the CUDA vector types header first, if we're compiling with nvcc. */ # include <vector_types.h> #endif /* Memory alignment macros mirroring those used by CUDA. */ #if !(defined(__VECTOR_TYPES_H__) || defined(__CUDACC__)) # if defined(__GNUC__) # define __align__(n) __attribute__((aligned(n))) # elif defined(_MSC_VER) # define __align__(n) __declspec(align(n)) # endif # if defined(__GNUC__) || defined(_WIN64) # define __builtin_align__(a) __align__(a) # else # define __builtin_align__(a) # endif #endif #if !(defined(__VECTOR_TYPES_H__) || defined(__CUDACC__)) /** * @brief * Two-element structure (single precision). * * @details * Structure used to hold data for a length-2 vector. * This must be compatible with the CUDA float2 type. */ struct __builtin_align__(8) float2 { float x, y; }; typedef struct float2 float2; #endif /** * @brief * Four-element complex structure (single precision). * * @details * Structure used to hold data for a length-4 single precision complex vector. * When used as a matrix, the elements should be interpreted as: * * ( a b ) * ( c d ) */ struct __builtin_align__(8) float4c { float2 a, b, c, d; }; typedef struct float4c float4c; #if !(defined(__VECTOR_TYPES_H__) || defined(__CUDACC__)) /** * @brief * Two-element structure (double precision). * * @details * Structure used to hold data for a length-2 vector. * This must be compatible with the CUDA double2 type. */ struct __builtin_align__(16) double2 { double x, y; }; typedef struct double2 double2; #endif /** * @brief * Four-element complex structure (double precision). * * @details * Structure used to hold data for a length-4 double precision complex vector. * When used as a matrix, the elements should be interpreted as: * * ( a b ) * ( c d ) */ struct __builtin_align__(16) double4c { double2 a, b, c, d; }; typedef struct double4c double4c; #ifdef __cplusplus } #endif #endif /* OSKAR_VECTOR_TYPES_H_ */
davepallot/OSKAR
oskar/vis/private_vis_header.h
/* * Copyright (c) 2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_PRIVATE_VIS_HEADER_H_ #define OSKAR_PRIVATE_VIS_HEADER_H_ #include <mem/oskar_mem.h> /* * Holds visibility header data, including station coordinates. * Arrays here will always be in CPU memory. */ struct oskar_VisHeader { oskar_Mem* settings; /* Settings file contents. */ oskar_Mem* telescope_path; /* Path to telescope model. */ int num_tags_per_block; /* Number of tags per block in binary file. */ int write_autocorr; /* True if auto-correlations are written. */ int write_crosscorr; /* True if cross-correlations are written. */ int amp_type; /* Memory type of the amplitudes. */ int coord_precision; /* Memory type of the coordinates. */ int max_times_per_block; /* Maximum no. time samples per block. */ int num_times_total; /* Total no. time samples. */ int max_channels_per_block; /* Maximum no. channels per block. */ int num_channels_total; /* Total no. channels. */ int num_stations; /* No. interferometer stations. */ int pol_type; /* Polarisation type enumerator. */ int phase_centre_type; /* Phase centre coordinate type. */ double phase_centre_deg[2]; /* Phase centre coordinates [deg]. */ double freq_start_hz; /* Start frequency [Hz]. */ double freq_inc_hz; /* Frequency increment [Hz]. */ double channel_bandwidth_hz; /* Frequency channel bandwidth [Hz]. */ double time_start_mjd_utc; /* Start time [MJD(UTC)]. */ double time_inc_sec; /* Smallest time increment [s]. */ double time_average_sec; /* Time average smearing duration [s]. */ double telescope_centre_lon_deg; /* Telescope reference longitude [deg]. */ double telescope_centre_lat_deg; /* Telescope reference latitude [deg]. */ double telescope_centre_alt_m; /* Telescope reference altitude [m]. */ oskar_Mem* station_x_offset_ecef_metres; /* Station x-coordinate, in metres (offset ECEF). */ oskar_Mem* station_y_offset_ecef_metres; /* Station y-coordinate, in metres (offset ECEF). */ oskar_Mem* station_z_offset_ecef_metres; /* Station z-coordinate, in metres (offset ECEF). */ }; #ifndef OSKAR_VIS_HEADER_TYPEDEF_ #define OSKAR_VIS_HEADER_TYPEDEF_ typedef struct oskar_VisHeader oskar_VisHeader; #endif /* OSKAR_VIS_HEADER_TYPEDEF_ */ #endif /* OSKAR_PRIVATE_VIS_HEADER_H_ */
davepallot/OSKAR
oskar/convert/private_convert_cirs_relative_directions_to_enu_directions_inline.h
<filename>oskar/convert/private_convert_cirs_relative_directions_to_enu_directions_inline.h<gh_stars>1-10 /* * Copyright (c) 2014, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_PRIVATE_CONVERT_CIRS_RELATIVE_DIRECTIONS_TO_ENU_DIRECTIONS_INLINE_H_ #define OSKAR_PRIVATE_CONVERT_CIRS_RELATIVE_DIRECTIONS_TO_ENU_DIRECTIONS_INLINE_H_ #include <oskar_global.h> #ifdef __cplusplus extern "C" { #endif /* Single precision. */ OSKAR_INLINE void oskar_convert_cirs_relative_directions_to_enu_directions_inline_f( float l, float m, float n, const float sin_lat, const float cos_lat, const float sin_ha0, const float cos_ha0, const float sin_dec0, const float cos_dec0, const float local_pm_x, const float local_pm_y, const float diurnal_aberration, float* x, float* y, float* z) { float f, x2, y2, z2, t1, t2; /* CIRS relative directions to Cartesian -HA, Dec. */ /* This is the first two stages of the original transformation: * rotate by -dec0 about x, then rotate by -ha0 about y. * INTERNAL: Axes are then permuted so that x -> Y, y -> Z and z -> X. * X towards local meridian, Z towards NCP, Y towards local east. */ t1 = m * sin_dec0; t2 = n * cos_dec0; y2 = l * cos_ha0 + t1 * sin_ha0 - t2 * sin_ha0; z2 = m * cos_dec0 + n * sin_dec0; x2 = l * sin_ha0 - t1 * cos_ha0 + t2 * cos_ha0; /* Polar motion. */ l = x2 + local_pm_x * z2; m = y2 - local_pm_y * z2; n = z2 - local_pm_x * x2 + local_pm_y * y2; /* Diurnal aberration. */ f = 1.0f - diurnal_aberration * m; x2 = f * l; y2 = f * (m + diurnal_aberration); z2 = f * n; /* Cartesian -HA, Dec to Cartesian ENU directions. */ /* This is the final (latitude) stage of the original transformation. */ *x = y2; *y = -(sin_lat * x2 - cos_lat * z2); *z = cos_lat * x2 + sin_lat * z2; } /* Double precision. */ OSKAR_INLINE void oskar_convert_cirs_relative_directions_to_enu_directions_inline_d( double l, double m, double n, const double sin_lat, const double cos_lat, const double sin_ha0, const double cos_ha0, const double sin_dec0, const double cos_dec0, const double local_pm_x, const double local_pm_y, const double diurnal_aberration, double* x, double* y, double* z) { double f, x2, y2, z2, t1, t2; /* CIRS relative directions to Cartesian -HA, Dec. */ /* This is the first two stages of the original transformation: * rotate by -dec0 about x, then rotate by -ha0 about y. * INTERNAL: Axes are then permuted so that x -> Y, y -> Z and z -> X. * X towards local meridian, Z towards NCP, Y towards local east. */ t1 = m * sin_dec0; t2 = n * cos_dec0; y2 = l * cos_ha0 + t1 * sin_ha0 - t2 * sin_ha0; z2 = m * cos_dec0 + n * sin_dec0; x2 = l * sin_ha0 - t1 * cos_ha0 + t2 * cos_ha0; /* Polar motion. */ l = x2 + local_pm_x * z2; m = y2 - local_pm_y * z2; n = z2 - local_pm_x * x2 + local_pm_y * y2; /* Diurnal aberration. */ f = 1.0 - diurnal_aberration * m; x2 = f * l; y2 = f * (m + diurnal_aberration); z2 = f * n; /* Cartesian -HA, Dec to Cartesian ENU directions. */ /* This is the final (latitude) stage of the original transformation. */ *x = y2; *y = -(sin_lat * x2 - cos_lat * z2); *z = cos_lat * x2 + sin_lat * z2; } #ifdef __cplusplus } #endif #endif /* OSKAR_PRIVATE_CONVERT_CIRS_RELATIVE_DIRECTIONS_TO_ENU_DIRECTIONS_INLINE_H_ */
davepallot/OSKAR
oskar/mem/src/oskar_mem_random_gaussian.c
/* * Copyright (c) 2015-2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "mem/oskar_mem.h" #include "mem/oskar_mem_random_gaussian_cuda.h" #include "math/private_random_helpers.h" #include "utility/oskar_cl_utils.h" #include "utility/oskar_device_utils.h" #ifdef __cplusplus extern "C" { #endif void oskar_mem_random_gaussian_f( const int num_elements, float* data, const unsigned int seed, const unsigned int counter1, const unsigned int counter2, const unsigned int counter3, const float std) { int i, i4, n1; n1 = num_elements / 4; #pragma omp parallel for private(i, i4) for (i = 0; i < n1; ++i) { float t[4]; OSKAR_R123_GENERATE_4(seed, i, counter1, counter2, counter3) /* Convert to normalised Gaussian distribution. */ i4 = i * 4; oskar_box_muller_f(u.i[0], u.i[1], &t[0], &t[1]); oskar_box_muller_f(u.i[2], u.i[3], &t[2], &t[3]); t[0] = std * t[0]; t[1] = std * t[1]; t[2] = std * t[2]; t[3] = std * t[3]; /* Store. */ data[i4] = t[0]; data[i4 + 1] = t[1]; data[i4 + 2] = t[2]; data[i4 + 3] = t[3]; } if (num_elements % 4) { float t[4]; OSKAR_R123_GENERATE_4(seed, n1, counter1, counter2, counter3) /* Convert to normalised Gaussian distribution. */ i4 = n1 * 4; oskar_box_muller_f(u.i[0], u.i[1], &t[0], &t[1]); oskar_box_muller_f(u.i[2], u.i[3], &t[2], &t[3]); t[0] = std * t[0]; t[1] = std * t[1]; t[2] = std * t[2]; t[3] = std * t[3]; /* Store. */ data[i4] = t[0]; if (i4 + 1 < num_elements) data[i4 + 1] = t[1]; if (i4 + 2 < num_elements) data[i4 + 2] = t[2]; if (i4 + 3 < num_elements) data[i4 + 3] = t[3]; } } void oskar_mem_random_gaussian_d( const int num_elements, double* data, const unsigned int seed, const unsigned int counter1, const unsigned int counter2, const unsigned int counter3, const double std) { int i, i4, n1; n1 = num_elements / 4; #pragma omp parallel for private(i, i4) for (i = 0; i < n1; ++i) { double t[4]; OSKAR_R123_GENERATE_4(seed, i, counter1, counter2, counter3) /* Convert to normalised Gaussian distribution. */ i4 = i * 4; oskar_box_muller_d(u.i[0], u.i[1], &t[0], &t[1]); oskar_box_muller_d(u.i[2], u.i[3], &t[2], &t[3]); t[0] = std * t[0]; t[1] = std * t[1]; t[2] = std * t[2]; t[3] = std * t[3]; /* Store. */ data[i4] = t[0]; data[i4 + 1] = t[1]; data[i4 + 2] = t[2]; data[i4 + 3] = t[3]; } if (num_elements % 4) { double t[4]; OSKAR_R123_GENERATE_4(seed, n1, counter1, counter2, counter3) /* Convert to normalised Gaussian distribution. */ i4 = n1 * 4; oskar_box_muller_d(u.i[0], u.i[1], &t[0], &t[1]); oskar_box_muller_d(u.i[2], u.i[3], &t[2], &t[3]); t[0] = std * t[0]; t[1] = std * t[1]; t[2] = std * t[2]; t[3] = std * t[3]; /* Store. */ data[i4] = t[0]; if (i4 + 1 < num_elements) data[i4 + 1] = t[1]; if (i4 + 2 < num_elements) data[i4 + 2] = t[2]; if (i4 + 3 < num_elements) data[i4 + 3] = t[3]; } } void oskar_mem_random_gaussian(oskar_Mem* data, unsigned int seed, unsigned int counter1, unsigned int counter2, unsigned int counter3, double std, int* status) { int type, location; size_t num_elements; #ifdef OSKAR_HAVE_OPENCL cl_kernel k = 0; #endif /* Check if safe to proceed. */ if (*status) return; type = oskar_mem_precision(data); location = oskar_mem_location(data); num_elements = oskar_mem_length(data); if (oskar_mem_is_complex(data)) num_elements *= 2; if (oskar_mem_is_matrix(data)) num_elements *= 4; if (location == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA if (type == OSKAR_SINGLE) oskar_mem_random_gaussian_cuda_f((int)num_elements, oskar_mem_float(data, status), seed, counter1, counter2, counter3, (float)std); else if (type == OSKAR_DOUBLE) oskar_mem_random_gaussian_cuda_d((int)num_elements, oskar_mem_double(data, status), seed, counter1, counter2, counter3, std); oskar_device_check_error(status); #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } else if (location == OSKAR_CPU) { if (type == OSKAR_SINGLE) oskar_mem_random_gaussian_f((int)num_elements, oskar_mem_float(data, status), seed, counter1, counter2, counter3, (float)std); else if (type == OSKAR_DOUBLE) oskar_mem_random_gaussian_d((int)num_elements, oskar_mem_double(data, status), seed, counter1, counter2, counter3, std); } else if (location & OSKAR_CL) { #ifdef OSKAR_HAVE_OPENCL if (type == OSKAR_SINGLE) k = oskar_cl_kernel("mem_random_gaussian_float"); else if (type == OSKAR_DOUBLE) k = oskar_cl_kernel("mem_random_gaussian_double"); if (k) { cl_device_type dev_type; cl_event event; cl_int error, gpu; cl_uint n, s, c1, c2, c3; size_t global_size, local_size; /* Set kernel arguments. */ clGetDeviceInfo(oskar_cl_device_id(), CL_DEVICE_TYPE, sizeof(cl_device_type), &dev_type, NULL); gpu = dev_type & CL_DEVICE_TYPE_GPU; n = (cl_uint) num_elements; s = (cl_uint) seed; c1 = (cl_uint) counter1; c2 = (cl_uint) counter2; c3 = (cl_uint) counter3; error = clSetKernelArg(k, 0, sizeof(cl_uint), &n); error |= clSetKernelArg(k, 1, sizeof(cl_mem), oskar_mem_cl_buffer(data, status)); error |= clSetKernelArg(k, 2, sizeof(cl_uint), &s); error |= clSetKernelArg(k, 3, sizeof(cl_uint), &c1); error |= clSetKernelArg(k, 4, sizeof(cl_uint), &c2); error |= clSetKernelArg(k, 5, sizeof(cl_uint), &c3); if (type == OSKAR_SINGLE) { cl_float t = (cl_float) std; error |= clSetKernelArg(k, 6, sizeof(cl_float), &t); } else if (type == OSKAR_DOUBLE) { cl_double t = (cl_double) std; error |= clSetKernelArg(k, 6, sizeof(cl_double), &t); } if (*status) return; if (error != CL_SUCCESS) { *status = OSKAR_ERR_INVALID_ARGUMENT; return; } /* Launch kernel on current command queue. */ local_size = gpu ? 256 : 128; global_size = ((((num_elements + 3) / 4) + local_size - 1) / local_size) * local_size; error = clEnqueueNDRangeKernel(oskar_cl_command_queue(), k, 1, NULL, &global_size, &local_size, 0, NULL, &event); if (error != CL_SUCCESS) { *status = OSKAR_ERR_KERNEL_LAUNCH_FAILURE; return; } } else { *status = OSKAR_ERR_FUNCTION_NOT_AVAILABLE; } #else *status = OSKAR_ERR_OPENCL_NOT_AVAILABLE; #endif } else *status = OSKAR_ERR_BAD_LOCATION; } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/telescope/src/oskar_telescope_set_station_coords_enu.c
<reponame>davepallot/OSKAR<filename>oskar/telescope/src/oskar_telescope_set_station_coords_enu.c /* * Copyright (c) 2016, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "convert/oskar_convert_ecef_to_geodetic_spherical.h" #include "convert/oskar_convert_enu_to_offset_ecef.h" #include "convert/oskar_convert_offset_ecef_to_ecef.h" #include "telescope/oskar_telescope.h" #ifdef __cplusplus extern "C" { #endif void oskar_telescope_set_station_coords_enu(oskar_Telescope* telescope, double longitude_rad, double latitude_rad, double altitude_m, int num_stations, const oskar_Mem* x, const oskar_Mem* y, const oskar_Mem* z, const oskar_Mem* x_err, const oskar_Mem* y_err, const oskar_Mem* z_err, int* status) { int i; /* Check lengths. */ if ((int)oskar_mem_length(x) < num_stations || (int)oskar_mem_length(y) < num_stations || (int)oskar_mem_length(z) < num_stations || (int)oskar_mem_length(x_err) < num_stations || (int)oskar_mem_length(y_err) < num_stations || (int)oskar_mem_length(z_err) < num_stations) { *status = OSKAR_ERR_DIMENSION_MISMATCH; return; } /* Resize the telescope model to hold the station data. */ oskar_telescope_resize(telescope, num_stations, status); if (*status) return; /* Store the telescope centre longitude, latitude, and altitude. */ oskar_telescope_set_position(telescope, longitude_rad, latitude_rad, altitude_m); /* Loop over station coordinates. */ for (i = 0; i < num_stations; ++i) { double lon = 0.0, lat = 0.0, alt = 0.0; /* x, y, z, delta x, delta y, delta z */ double hor[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; double ecef[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; /* Get coordinates from input arrays. */ hor[0] = oskar_mem_get_element(x, i, status); hor[1] = oskar_mem_get_element(y, i, status); hor[2] = oskar_mem_get_element(z, i, status); hor[3] = oskar_mem_get_element(x_err, i, status); hor[4] = oskar_mem_get_element(y_err, i, status); hor[5] = oskar_mem_get_element(z_err, i, status); /* Get "true" coordinates ([3, 4, 5]) from "measured" coordinates. */ hor[3] += hor[0]; hor[4] += hor[1]; hor[5] += hor[2]; /* Convert horizon plane to offset geocentric Cartesian coordinates. */ oskar_convert_enu_to_offset_ecef_d(1, &hor[0], &hor[1], &hor[2], longitude_rad, latitude_rad, &ecef[0], &ecef[1], &ecef[2]); oskar_convert_enu_to_offset_ecef_d(1, &hor[3], &hor[4], &hor[5], longitude_rad, latitude_rad, &ecef[3], &ecef[4], &ecef[5]); /* Store the offset geocentric and horizon plane coordinates. */ oskar_telescope_set_station_coords(telescope, i, &ecef[0], &ecef[3], &hor[0], &hor[3], status); if (*status) break; /* Convert to ECEF, then to station longitude, latitude, altitude. */ oskar_convert_offset_ecef_to_ecef(1, &ecef[3], &ecef[4], &ecef[5], longitude_rad, latitude_rad, altitude_m, &ecef[3], &ecef[4], &ecef[5]); oskar_convert_ecef_to_geodetic_spherical(1, &ecef[3], &ecef[4], &ecef[5], &lon, &lat, &alt); oskar_station_set_position(oskar_telescope_station(telescope, i), lon, lat, alt); } /* (Re-)Set unique station IDs. */ oskar_telescope_set_station_ids(telescope); } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/math/oskar_rotate.h
<reponame>davepallot/OSKAR<filename>oskar/math/oskar_rotate.h /* * Copyright (c) 2012-2013, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_ROTATE_H_ #define OSKAR_ROTATE_H_ /** * @file oskar_rotate.h */ #include <oskar_global.h> #ifdef __cplusplus extern "C" { #endif /** * @brief * Rotate on a sphere by the specified longitude and latitude * (single precision). * * @details * Rotates points on the surface of a sphere by the specified longitude * and latitude. * * @param[in] num_points Number of points to rotate. * @param[in,out] x The x-coordinates. * @param[in,out] y The y-coordinates. * @param[in,out] z The z-coordinates. * @param[in] lon East-positive longitude rotation angle, in radians. * @param[in] lat North-positive latitude rotation angle, in radians. */ OSKAR_EXPORT void oskar_rotate_sph_f(int num_points, float* x, float* y, float*z, float lon, float lat); /** * @brief * Rotate on a sphere by the specified longitude and latitude * (double precision). * * @details * Rotates points on the surface of a sphere by the specified longitude * and latitude. * * @param[in] num_points Number of points to rotate. * @param[in,out] x The x-coordinates. * @param[in,out] y The y-coordinates. * @param[in,out] z The z-coordinates. * @param[in] lon East-positive longitude rotation angle, in radians. * @param[in] lat North-positive latitude rotation angle, in radians. */ OSKAR_EXPORT void oskar_rotate_sph_d(int num_points, double* x, double* y, double*z, double lon, double lat); /** * @brief * Rotates points around the the x-axis (single precision). * * @details * Rotates a set of points by a given angle about the x-axis. * * @param[in] num_points Number of points to rotate. * @param[in,out] y The y-coordinates. * @param[in,out] z The z-coordinates. * @param[in] angle Rotation angle, in radians. */ OSKAR_EXPORT void oskar_rotate_x_f(int num_points, float* y, float* z, float angle); /** * @brief * Rotates points around the the y-axis (single precision). * * @details * Rotates a set of points by a given angle about the y-axis. * * @param[in] num_points Number of points to rotate. * @param[in,out] x The x-coordinates. * @param[in,out] z The z-coordinates. * @param[in] angle Rotation angle, in radians. */ OSKAR_EXPORT void oskar_rotate_y_f(int num_points, float* x, float* z, float angle); /** * @brief * Rotates points around the the z-axis (single precision). * * @details * Rotates a set of points by a given angle about the z-axis. * * @param[in] num_points Number of points to rotate. * @param[in,out] x The x-coordinates. * @param[in,out] y The y-coordinates. * @param[in] angle Rotation angle, in radians. */ OSKAR_EXPORT void oskar_rotate_z_f(int num_points, float* x, float* y, float angle); /** * @brief * Rotates points around the the x-axis (double precision). * * @details * Rotates a set of points by a given angle about the x-axis. * * @param[in] num_points Number of points to rotate. * @param[in,out] y The y-coordinates. * @param[in,out] z The z-coordinates. * @param[in] angle Rotation angle, in radians. */ OSKAR_EXPORT void oskar_rotate_x_d(int num_points, double* y, double* z, double angle); /** * @brief * Rotates points around the the y-axis (double precision). * * @details * Rotates a set of points by a given angle about the y-axis. * * @param[in] num_points Number of points to rotate. * @param[in,out] x The x-coordinates. * @param[in,out] z The z-coordinates. * @param[in] angle Rotation angle, in radians. */ OSKAR_EXPORT void oskar_rotate_y_d(int num_points, double* x, double* z, double angle); /** * @brief * Rotates points around the the z-axis (double precision). * * @details * Rotates a set of points by a given angle about the z-axis. * * @param[in] num_points Number of points to rotate. * @param[in,out] x The x-coordinates. * @param[in,out] y The y-coordinates. * @param[in] angle Rotation angle, in radians. */ OSKAR_EXPORT void oskar_rotate_z_d(int num_points, double* x, double* y, double angle); #ifdef __cplusplus } #endif #endif /* OSKAR_ROTATE_H_ */
davepallot/OSKAR
oskar/log/src/oskar_log_create.c
/* * Copyright (c) 2012-2016, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "log/private_log.h" #include "log/oskar_log.h" #include "oskar_version.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef OSKAR_OS_WIN #define WIN32_LEAN_AND_MEAN #include <windows.h> #else #include <unistd.h> #endif #ifdef __cplusplus extern "C" { #endif oskar_Log* oskar_log_create(int file_priority, int term_priority) { oskar_Log* log = 0; char fname1[64], fname2[64]; char* current_dir = 0; size_t buf_len = 0; int time_data[10]; int i = 0, n = 0; /* Allocate the structure. */ log = (oskar_Log*) malloc(sizeof(oskar_Log)); if (!log) return 0; /* Initialise memory for the log data. */ log->keep_file = 1; log->file_priority = file_priority; log->term_priority = term_priority; log->name = 0; log->code = 0; log->offset = 0; log->length = 0; log->timestamp = 0; log->size = 0; log->capacity = 0; log->file = 0; log->value_width = OSKAR_LOG_DEFAULT_VALUE_WIDTH; /* Get the system time information. */ oskar_log_system_clock_data(0, time_data); /* Construct log file name root. */ #if __STDC_VERSION__ >= 199901L n = snprintf(fname1, sizeof(fname1), "oskar_%.4d-%.2d-%.2d_%.2d%.2d%.2d", time_data[5], time_data[4], time_data[3], time_data[2], time_data[1], time_data[0]); #else n = sprintf(fname1, "oskar_%.4d-%.2d-%.2d_%.2d%.2d%.2d", time_data[5], time_data[4], time_data[3], time_data[2], time_data[1], time_data[0]); #endif if (n < 0 || n >= (int)sizeof(fname1)) return 0; /* Construct a unique log file name. */ do { ++i; if (i == 1) { #if __STDC_VERSION__ >= 199901L n = snprintf(fname2, sizeof(fname2), "%s.log", fname1); #else n = sprintf(fname2, "%s.log", fname1); #endif } else { #if __STDC_VERSION__ >= 199901L n = snprintf(fname2, sizeof(fname2), "%s_%d.log", fname1, i); #else n = sprintf(fname2, "%s_%d.log", fname1, i); #endif } if (n < 0 || n >= (int)sizeof(fname1)) return 0; } while (oskar_log_file_exists(fname2)); /* Open the log file for appending, and save the file handle. */ log->file = fopen(fname2, "a+"); /* Write standard header. */ oskar_log_section(log, 'M', "OSKAR-%s starting at %s.", OSKAR_VERSION_STR, oskar_log_system_clock_string(0)); /* Get the current working directory. */ #ifdef OSKAR_OS_WIN buf_len = GetCurrentDirectory(0, NULL); current_dir = (char*) calloc(buf_len, sizeof(char)); GetCurrentDirectory((DWORD) buf_len, current_dir); #else do { buf_len += 256; current_dir = (char*) realloc(current_dir, buf_len); } while (getcwd(current_dir, buf_len) == NULL); #endif oskar_log_message(log, 'M', 0, "Current dir is %s", current_dir); free(current_dir); /* Write a message to say if the log file was opened successfully. */ if (log->file) { /* Save the file name in the structure. */ log->name = (char*) calloc(n + 1, sizeof(char)); strcpy(log->name, fname2); oskar_log_message(log, 'M', 0, "Logging to file %s", fname2); } else { oskar_log_warning(log, "File error: log file could not be created."); } return log; } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/correlate/oskar_evaluate_auto_power_cuda.h
/* * Copyright (c) 2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_EVALUATE_AUTO_POWER_CUDA_H_ #define OSKAR_EVALUATE_AUTO_POWER_CUDA_H_ /** * @file oskar_evaluate_auto_power_cuda.h */ #include <oskar_global.h> #include <utility/oskar_vector_types.h> #ifdef __cplusplus extern "C" { #endif /** * @brief * CUDA function to evaluate the auto-power product for a station * (single precision). * * @details * This function evaluates the auto-power product for the supplied * source list. * * Note that all pointers are device pointers, and must not be dereferenced * in host code. * * @param[in] num_sources The number of sources in the input arrays. * @param[in] d_jones Pointer to Jones matrix list * (length \p num_sources). * @param[out] d_out Pointer to output auto-power product * (length \p num_sources). */ OSKAR_EXPORT void oskar_evaluate_auto_power_cuda_f(int num_sources, const float4c* d_jones, float4c* d_out); /** * @brief * CUDA function to evaluate the auto-power product for a station * (single precision). * * @details * This function evaluates the auto-power product for the supplied * source list. * * Note that all pointers are device pointers, and must not be dereferenced * in host code. * * @param[in] num_sources The number of sources in the input arrays. * @param[in] d_jones Pointer to Jones matrix list * (length \p num_sources). * @param[out] d_out Pointer to output auto-power product * (length \p num_sources). */ OSKAR_EXPORT void oskar_evaluate_auto_power_scalar_cuda_f(int num_sources, const float2* d_jones, float2* d_out); /** * @brief * CUDA function to evaluate the auto-power product for a station * (double precision). * * @details * This function evaluates the auto-power product for the supplied * source list. * * Note that all pointers are device pointers, and must not be dereferenced * in host code. * * @param[in] num_sources The number of sources in the input arrays. * @param[in] d_jones Pointer to Jones matrix list * (length \p num_sources). * @param[out] d_out Pointer to output auto-power product * (length \p num_sources). */ OSKAR_EXPORT void oskar_evaluate_auto_power_cuda_d(int num_sources, const double4c* d_jones, double4c* d_out); /** * @brief * CUDA function to evaluate the auto-power product for a station * (double precision). * * @details * This function evaluates the auto-power product for the supplied * source list. * * Note that all pointers are device pointers, and must not be dereferenced * in host code. * * @param[in] num_sources The number of sources in the input arrays. * @param[in] d_jones Pointer to Jones matrix list * (length \p num_sources). * @param[out] d_out Pointer to output auto-power product * (length \p num_sources). */ OSKAR_EXPORT void oskar_evaluate_auto_power_scalar_cuda_d(int num_sources, const double2* d_jones, double2* d_out); #ifdef __cplusplus } #endif #endif /* OSKAR_EVALUATE_AUTO_POWER_CUDA_H_ */
davepallot/OSKAR
oskar/telescope/station/src/oskar_station_create_copy.c
<reponame>davepallot/OSKAR<filename>oskar/telescope/station/src/oskar_station_create_copy.c /* * Copyright (c) 2013-2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "telescope/station/private_station.h" #include "telescope/station/oskar_station.h" #include "telescope/station/element/oskar_element_copy.h" #include <stdlib.h> #include <stddef.h> #include <stdio.h> #ifdef __cplusplus extern "C" { #endif oskar_Station* oskar_station_create_copy(const oskar_Station* src, int location, int* status) { int i = 0; oskar_Station* model = 0; /* Check if safe to proceed. */ if (*status) return model; /* Create the new model. */ model = oskar_station_create(oskar_station_precision(src), location, oskar_station_num_elements(src), status); /* Set meta-data. */ model->unique_id = src->unique_id; model->precision = src->precision; model->mem_location = location; /* Copy common station parameters. */ model->station_type = src->station_type; model->normalise_final_beam = src->normalise_final_beam; model->lon_rad = src->lon_rad; model->lat_rad = src->lat_rad; model->alt_metres = src->alt_metres; model->pm_x_rad = src->pm_x_rad; model->pm_y_rad = src->pm_y_rad; model->beam_lon_rad = src->beam_lon_rad; model->beam_lat_rad = src->beam_lat_rad; model->beam_coord_type = src->beam_coord_type; oskar_mem_copy(model->noise_freq_hz, src->noise_freq_hz, status); oskar_mem_copy(model->noise_rms_jy, src->noise_rms_jy, status); /* Copy aperture array data, except num_element_types (done later). */ model->identical_children = src->identical_children; model->num_elements = src->num_elements; model->normalise_array_pattern = src->normalise_array_pattern; model->enable_array_pattern = src->enable_array_pattern; model->common_element_orientation = src->common_element_orientation; model->array_is_3d = src->array_is_3d; model->apply_element_errors = src->apply_element_errors; model->apply_element_weight = src->apply_element_weight; model->seed_time_variable_errors = src->seed_time_variable_errors; model->num_permitted_beams = src->num_permitted_beams; /* Copy Gaussian station beam data. */ model->gaussian_beam_fwhm_rad = src->gaussian_beam_fwhm_rad; model->gaussian_beam_reference_freq_hz = src->gaussian_beam_reference_freq_hz; /* Copy memory blocks. */ oskar_mem_copy(model->element_true_x_enu_metres, src->element_true_x_enu_metres, status); oskar_mem_copy(model->element_true_y_enu_metres, src->element_true_y_enu_metres, status); oskar_mem_copy(model->element_true_z_enu_metres, src->element_true_z_enu_metres, status); oskar_mem_copy(model->element_measured_x_enu_metres, src->element_measured_x_enu_metres, status); oskar_mem_copy(model->element_measured_y_enu_metres, src->element_measured_y_enu_metres, status); oskar_mem_copy(model->element_measured_z_enu_metres, src->element_measured_z_enu_metres, status); oskar_mem_copy(model->element_weight, src->element_weight, status); oskar_mem_copy(model->element_gain, src->element_gain, status); oskar_mem_copy(model->element_gain_error, src->element_gain_error, status); oskar_mem_copy(model->element_phase_offset_rad, src->element_phase_offset_rad, status); oskar_mem_copy(model->element_phase_error_rad, src->element_phase_error_rad, status); oskar_mem_copy(model->element_x_alpha_cpu, src->element_x_alpha_cpu, status); oskar_mem_copy(model->element_x_beta_cpu, src->element_x_beta_cpu, status); oskar_mem_copy(model->element_x_gamma_cpu, src->element_x_gamma_cpu, status); oskar_mem_copy(model->element_y_alpha_cpu, src->element_y_alpha_cpu, status); oskar_mem_copy(model->element_y_beta_cpu, src->element_y_beta_cpu, status); oskar_mem_copy(model->element_y_gamma_cpu, src->element_y_gamma_cpu, status); oskar_mem_copy(model->element_types, src->element_types, status); oskar_mem_copy(model->element_types_cpu, src->element_types_cpu, status); oskar_mem_copy(model->element_mount_types_cpu, src->element_mount_types_cpu, status); oskar_mem_copy(model->permitted_beam_az_rad, src->permitted_beam_az_rad, status); oskar_mem_copy(model->permitted_beam_el_rad, src->permitted_beam_el_rad, status); /* Copy element models, if set. */ if (oskar_station_has_element(src)) { /* Ensure enough space for element model data. */ oskar_station_resize_element_types(model, src->num_element_types, status); /* Copy the element model data. */ for (i = 0; i < src->num_element_types; ++i) { oskar_element_copy(model->element[i], src->element[i], status); } } /* Recursively copy child stations. */ if (oskar_station_has_child(src)) { model->child = malloc(src->num_elements * sizeof(oskar_Station*)); if (!model->child) { *status = OSKAR_ERR_MEMORY_ALLOC_FAILURE; return model; } for (i = 0; i < src->num_elements; ++i) { model->child[i] = oskar_station_create_copy( oskar_station_child_const(src, i), location, status); } } return model; } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/sky/src/oskar_evaluate_tec_tid.c
/* * Copyright (c) 2013-2014, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "sky/oskar_evaluate_tec_tid.h" #include "math/oskar_cmath.h" #include <stdio.h> #ifdef __cplusplus extern "C" { #endif void oskar_evaluate_tec_tid(oskar_Mem* tec, int num_directions, const oskar_Mem* lon, const oskar_Mem* lat, const oskar_Mem* rel_path_length, double TEC0, oskar_SettingsTIDscreen* TID, double gast) { int i, j, type; double pp_lon, pp_lat; double pp_sec; double pp_tec; double amp, w, th, v; /* TID parameters */ double time; double earth_radius = 6365.0; /* km -- FIXME */ int status = 0; /* TODO check types, dimensions etc of memory */ type = oskar_mem_type(tec); oskar_mem_set_value_real(tec, 0.0, 0, 0, &status); /* Loop over TIDs */ for (i = 0; i < TID->num_components; ++i) { amp = TID->amp[i]; /* convert from km to rads */ w = TID->wavelength[i] / (earth_radius + TID->height_km); th = TID->theta[i] * M_PI/180.; /* convert from km/h to rad/s */ v = (TID->speed[i]/(earth_radius + TID->height_km)) / 3600; time = gast * 86400.0; /* days->sec */ /* Loop over directions */ for (j = 0; j < num_directions; ++j) { if (type == OSKAR_DOUBLE) { pp_lon = oskar_mem_double_const(lon, &status)[j]; pp_lat = oskar_mem_double_const(lat, &status)[j]; pp_sec = oskar_mem_double_const(rel_path_length, &status)[j]; pp_tec = pp_sec * amp * TEC0 * ( cos( (2.0*M_PI/w) * (cos(th)*pp_lon - v*time) ) + cos( (2.0*M_PI/w) * (sin(th)*pp_lat - v*time) ) ); pp_tec += TEC0; oskar_mem_double(tec, &status)[j] += pp_tec; } else { pp_lon = (double)oskar_mem_float_const(lon, &status)[j]; pp_lat = (double)oskar_mem_float_const(lat, &status)[j]; pp_sec = (double)oskar_mem_float_const(rel_path_length, &status)[j]; pp_tec = pp_sec * amp * TEC0 * ( cos( (2.0*M_PI/w) * (cos(th)*pp_lon - v*time) ) + cos( (2.0*M_PI/w) * (sin(th)*pp_lat - v*time) ) ); pp_tec += TEC0; oskar_mem_float(tec, &status)[j] += (float)pp_tec; } } /* loop over directions */ } /* loop over components. */ } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/telescope/station/element/src/oskar_apply_element_taper_cosine.c
/* * Copyright (c) 2012-2018, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "telescope/station/element/oskar_apply_element_taper_cosine.h" #include "telescope/station/element/oskar_apply_element_taper_cosine_cuda.h" #include "utility/oskar_cl_utils.h" #include "utility/oskar_device_utils.h" #include <math.h> #ifdef __cplusplus extern "C" { #endif /* Single precision. */ void oskar_apply_element_taper_cosine_scalar_f(int num_sources, float cos_power, const float* theta, float2* jones) { int i; for (i = 0; i < num_sources; ++i) { const float f = powf(cosf(theta[i]), cos_power); jones[i].x *= f; jones[i].y *= f; } } void oskar_apply_element_taper_cosine_matrix_f(int num_sources, float cos_power, const float* theta, float4c* jones) { int i; for (i = 0; i < num_sources; ++i) { const float f = powf(cosf(theta[i]), cos_power); jones[i].a.x *= f; jones[i].a.y *= f; jones[i].b.x *= f; jones[i].b.y *= f; jones[i].c.x *= f; jones[i].c.y *= f; jones[i].d.x *= f; jones[i].d.y *= f; } } /* Double precision. */ void oskar_apply_element_taper_cosine_scalar_d(int num_sources, double cos_power, const double* theta, double2* jones) { int i; for (i = 0; i < num_sources; ++i) { const double f = pow(cos(theta[i]), cos_power); jones[i].x *= f; jones[i].y *= f; } } void oskar_apply_element_taper_cosine_matrix_d(int num_sources, double cos_power, const double* theta, double4c* jones) { int i; for (i = 0; i < num_sources; ++i) { const double f = pow(cos(theta[i]), cos_power); jones[i].a.x *= f; jones[i].a.y *= f; jones[i].b.x *= f; jones[i].b.y *= f; jones[i].c.x *= f; jones[i].c.y *= f; jones[i].d.x *= f; jones[i].d.y *= f; } } /* Wrapper. */ void oskar_apply_element_taper_cosine(oskar_Mem* jones, int num_sources, double cos_power, const oskar_Mem* theta, int* status) { if (*status) return; /* Check arrays are co-located. */ const int location = oskar_mem_location(jones); if (oskar_mem_location(theta) != location) { *status = OSKAR_ERR_LOCATION_MISMATCH; return; } /* Check types for consistency. */ if (oskar_mem_type(theta) != oskar_mem_precision(jones)) { *status = OSKAR_ERR_TYPE_MISMATCH; return; } /* Apply taper. */ if (location == OSKAR_CPU) { switch (oskar_mem_type(jones)) { case OSKAR_SINGLE_COMPLEX: oskar_apply_element_taper_cosine_scalar_f(num_sources, (float)cos_power, oskar_mem_float_const(theta, status), oskar_mem_float2(jones, status)); break; case OSKAR_DOUBLE_COMPLEX: oskar_apply_element_taper_cosine_scalar_d(num_sources, cos_power, oskar_mem_double_const(theta, status), oskar_mem_double2(jones, status)); break; case OSKAR_SINGLE_COMPLEX_MATRIX: oskar_apply_element_taper_cosine_matrix_f(num_sources, (float)cos_power, oskar_mem_float_const(theta, status), oskar_mem_float4c(jones, status)); break; case OSKAR_DOUBLE_COMPLEX_MATRIX: oskar_apply_element_taper_cosine_matrix_d(num_sources, cos_power, oskar_mem_double_const(theta, status), oskar_mem_double4c(jones, status)); break; default: *status = OSKAR_ERR_BAD_DATA_TYPE; break; } } else if (location == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA switch (oskar_mem_type(jones)) { case OSKAR_SINGLE_COMPLEX: oskar_apply_element_taper_cosine_scalar_cuda_f(num_sources, (float)cos_power, oskar_mem_float_const(theta, status), oskar_mem_float2(jones, status)); break; case OSKAR_DOUBLE_COMPLEX: oskar_apply_element_taper_cosine_scalar_cuda_d(num_sources, cos_power, oskar_mem_double_const(theta, status), oskar_mem_double2(jones, status)); break; case OSKAR_SINGLE_COMPLEX_MATRIX: oskar_apply_element_taper_cosine_matrix_cuda_f(num_sources, (float)cos_power, oskar_mem_float_const(theta, status), oskar_mem_float4c(jones, status)); break; case OSKAR_DOUBLE_COMPLEX_MATRIX: oskar_apply_element_taper_cosine_matrix_cuda_d(num_sources, cos_power, oskar_mem_double_const(theta, status), oskar_mem_double4c(jones, status)); break; default: *status = OSKAR_ERR_BAD_DATA_TYPE; break; } oskar_device_check_error(status); #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } else if (location & OSKAR_CL) { #ifdef OSKAR_HAVE_OPENCL cl_device_type dev_type; cl_event event; cl_kernel k = 0; cl_int is_gpu, error, num; cl_uint arg = 0; size_t global_size, local_size; clGetDeviceInfo(oskar_cl_device_id(), CL_DEVICE_TYPE, sizeof(cl_device_type), &dev_type, NULL); is_gpu = dev_type & CL_DEVICE_TYPE_GPU; switch (oskar_mem_type(jones)) { case OSKAR_SINGLE_COMPLEX: k = oskar_cl_kernel("apply_element_taper_cosine_scalar_float"); break; case OSKAR_DOUBLE_COMPLEX: k = oskar_cl_kernel("apply_element_taper_cosine_scalar_double"); break; case OSKAR_SINGLE_COMPLEX_MATRIX: k = oskar_cl_kernel("apply_element_taper_cosine_matrix_float"); break; case OSKAR_DOUBLE_COMPLEX_MATRIX: k = oskar_cl_kernel("apply_element_taper_cosine_matrix_double"); break; default: *status = OSKAR_ERR_BAD_DATA_TYPE; return; } if (!k) { *status = OSKAR_ERR_FUNCTION_NOT_AVAILABLE; return; } /* Set kernel arguments. */ num = (cl_int) num_sources; error = clSetKernelArg(k, arg++, sizeof(cl_int), &num); if (oskar_mem_precision(jones) == OSKAR_SINGLE) { cl_float w = (cl_float) cos_power; error = clSetKernelArg(k, arg++, sizeof(cl_float), &w); } else { cl_double w = (cl_double) cos_power; error = clSetKernelArg(k, arg++, sizeof(cl_double), &w); } error |= clSetKernelArg(k, arg++, sizeof(cl_mem), oskar_mem_cl_buffer_const(theta, status)); error |= clSetKernelArg(k, arg++, sizeof(cl_mem), oskar_mem_cl_buffer(jones, status)); if (error != CL_SUCCESS) { *status = OSKAR_ERR_INVALID_ARGUMENT; return; } /* Launch kernel on current command queue. */ local_size = is_gpu ? 256 : 128; global_size = ((num + local_size - 1) / local_size) * local_size; error = clEnqueueNDRangeKernel(oskar_cl_command_queue(), k, 1, NULL, &global_size, &local_size, 0, NULL, &event); if (error != CL_SUCCESS) *status = OSKAR_ERR_KERNEL_LAUNCH_FAILURE; #else *status = OSKAR_ERR_OPENCL_NOT_AVAILABLE; #endif } else *status = OSKAR_ERR_BAD_LOCATION; } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/splines/oskar_dierckx_bispev_cuda.h
<gh_stars>1-10 /* * Copyright (c) 2012-2014, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_DIERCKX_BISPEV_CUDA_H_ #define OSKAR_DIERCKX_BISPEV_CUDA_H_ /** * @file oskar_dierckx_bispev_cuda.h */ #include <oskar_global.h> #ifdef __cplusplus extern "C" { #endif /** * @brief * Function to evaluate B-spline coefficients using CUDA (single precision). * * @details * This function evaluates B-spline coefficients to determine * values of a fitted surface at the specified points using CUDA. * * Note that all pointers refer to device memory. * * @param[in] d_tx Array of knot positions in x. * @param[in] nx Number of knot positions in x. * @param[in] d_ty Array of knot positions in y. * @param[in] ny Number of knot positions in y. * @param[in] d_c Array of spline coefficients. * @param[in] kx Order of spline in x (use 3 for bicubic). * @param[in] ky Order of spline in y (use 3 for bicubic). * @param[in] n Number of points to evaluate. * @param[in] d_x Input x positions. * @param[in] d_y Input y positions. * @param[in] stride Memory stride of output values (use 1 for contiguous). * @param[out] d_z Output surface values. */ OSKAR_EXPORT void oskar_dierckx_bispev_cuda_f(const float* d_tx, int nx, const float* d_ty, int ny, const float* d_c, int kx, int ky, int n, const float* d_x, const float* d_y, int stride, float* d_z); /** * @brief * Function to evaluate B-spline coefficients using CUDA (double precision). * * @details * This function evaluates B-spline coefficients to determine * values of a fitted surface at the specified points using CUDA. * * Note that all pointers refer to device memory. * * @param[in] d_tx Array of knot positions in x. * @param[in] nx Number of knot positions in x. * @param[in] d_ty Array of knot positions in y. * @param[in] ny Number of knot positions in y. * @param[in] d_c Array of spline coefficients. * @param[in] kx Order of spline in x (use 3 for bicubic). * @param[in] ky Order of spline in y (use 3 for bicubic). * @param[in] n Number of points to evaluate. * @param[in] d_x Input x positions. * @param[in] d_y Input y positions. * @param[in] stride Memory stride of output values (use 1 for contiguous). * @param[out] d_z Output surface values. */ OSKAR_EXPORT void oskar_dierckx_bispev_cuda_d(const double* d_tx, int nx, const double* d_ty, int ny, const double* d_c, int kx, int ky, int n, const double* d_x, const double* d_y, int stride, double* d_z); #ifdef __CUDACC__ /* Kernels. */ __global__ void oskar_dierckx_bispev_cudak_f(const float* tx, const int nx, const float* ty, const int ny, const float* c, const int kx, const int ky, const int n, const float* x, const float* y, const int stride, float* z); __global__ void oskar_dierckx_bispev_cudak_d(const double* tx, const int nx, const double* ty, const int ny, const double* c, const int kx, const int ky, const int n, const double* x, const double* y, const int stride, double* z); #endif /* __CUDACC__ */ #ifdef __cplusplus } #endif #endif /* OSKAR_DIERCKX_BISPEV_CUDA_H_ */
davepallot/OSKAR
oskar/telescope/station/element/src/oskar_element_resize_freq_data.c
/* * Copyright (c) 2014-2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "telescope/station/element/private_element.h" #include "telescope/station/element/oskar_element.h" #ifdef __cplusplus extern "C" { #endif static void realloc_arrays(oskar_Element* e, int size, int* status); void oskar_element_resize_freq_data(oskar_Element* model, int size, int* status) { int i, old_size, loc, precision; /* Check if safe to proceed. */ if (*status) return; /* Get the old size. */ old_size = model->num_freq; loc = oskar_element_mem_location(model); precision = oskar_element_precision(model); if (size > old_size) { /* Enlarge the arrays and create new structures. */ realloc_arrays(model, size, status); for (i = old_size; i < size; ++i) { model->filename_x[i] = oskar_mem_create(OSKAR_CHAR, OSKAR_CPU, 0, status); model->filename_y[i] = oskar_mem_create(OSKAR_CHAR, OSKAR_CPU, 0, status); model->filename_scalar[i] = oskar_mem_create(OSKAR_CHAR, OSKAR_CPU, 0, status); model->x_v_re[i] = oskar_splines_create(precision, loc, status); model->x_v_im[i] = oskar_splines_create(precision, loc, status); model->x_h_re[i] = oskar_splines_create(precision, loc, status); model->x_h_im[i] = oskar_splines_create(precision, loc, status); model->y_v_re[i] = oskar_splines_create(precision, loc, status); model->y_v_im[i] = oskar_splines_create(precision, loc, status); model->y_h_re[i] = oskar_splines_create(precision, loc, status); model->y_h_im[i] = oskar_splines_create(precision, loc, status); model->scalar_re[i] = oskar_splines_create(precision, loc, status); model->scalar_im[i] = oskar_splines_create(precision, loc, status); } } else if (size < old_size) { /* Free old structures and shrink the arrays. */ for (i = size; i < old_size; ++i) { oskar_mem_free(model->filename_x[i], status); oskar_mem_free(model->filename_y[i], status); oskar_mem_free(model->filename_scalar[i], status); oskar_splines_free(model->x_v_re[i], status); oskar_splines_free(model->x_v_im[i], status); oskar_splines_free(model->x_h_re[i], status); oskar_splines_free(model->x_h_im[i], status); oskar_splines_free(model->y_v_re[i], status); oskar_splines_free(model->y_v_im[i], status); oskar_splines_free(model->y_h_re[i], status); oskar_splines_free(model->y_h_im[i], status); oskar_splines_free(model->scalar_re[i], status); oskar_splines_free(model->scalar_im[i], status); } realloc_arrays(model, size, status); } else { /* No resize necessary. */ return; } /* Store the new size. */ model->num_freq = size; } static void realloc_arrays(oskar_Element* e, int size, int* status) { e->freqs_hz = realloc(e->freqs_hz, size * sizeof(double)); if (!e->freqs_hz) *status = OSKAR_ERR_MEMORY_ALLOC_FAILURE; e->filename_x = realloc(e->filename_x, size * sizeof(oskar_Mem*)); if (!e->filename_x) *status = OSKAR_ERR_MEMORY_ALLOC_FAILURE; e->filename_y = realloc(e->filename_y, size * sizeof(oskar_Mem*)); if (!e->filename_y) *status = OSKAR_ERR_MEMORY_ALLOC_FAILURE; e->filename_scalar = realloc(e->filename_scalar, size * sizeof(oskar_Mem*)); if (!e->filename_scalar) *status = OSKAR_ERR_MEMORY_ALLOC_FAILURE; e->x_v_re = realloc(e->x_v_re, size * sizeof(oskar_Splines*)); if (!e->x_v_re) *status = OSKAR_ERR_MEMORY_ALLOC_FAILURE; e->x_v_im = realloc(e->x_v_im, size * sizeof(oskar_Splines*)); if (!e->x_v_im) *status = OSKAR_ERR_MEMORY_ALLOC_FAILURE; e->x_h_re = realloc(e->x_h_re, size * sizeof(oskar_Splines*)); if (!e->x_h_re) *status = OSKAR_ERR_MEMORY_ALLOC_FAILURE; e->x_h_im = realloc(e->x_h_im, size * sizeof(oskar_Splines*)); if (!e->x_h_im) *status = OSKAR_ERR_MEMORY_ALLOC_FAILURE; e->y_v_re = realloc(e->y_v_re, size * sizeof(oskar_Splines*)); if (!e->y_v_re) *status = OSKAR_ERR_MEMORY_ALLOC_FAILURE; e->y_v_im = realloc(e->y_v_im, size * sizeof(oskar_Splines*)); if (!e->y_v_im) *status = OSKAR_ERR_MEMORY_ALLOC_FAILURE; e->y_h_re = realloc(e->y_h_re, size * sizeof(oskar_Splines*)); if (!e->y_h_re) *status = OSKAR_ERR_MEMORY_ALLOC_FAILURE; e->y_h_im = realloc(e->y_h_im, size * sizeof(oskar_Splines*)); if (!e->y_h_im) *status = OSKAR_ERR_MEMORY_ALLOC_FAILURE; e->scalar_re = realloc(e->scalar_re, size * sizeof(oskar_Splines*)); if (!e->scalar_re) *status = OSKAR_ERR_MEMORY_ALLOC_FAILURE; e->scalar_im = realloc(e->scalar_im, size * sizeof(oskar_Splines*)); if (!e->scalar_im) *status = OSKAR_ERR_MEMORY_ALLOC_FAILURE; } #ifdef __cplusplus } #endif
davepallot/OSKAR
gui/oskar_MainWindow.h
/* * Copyright (c) 2012-2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_MAIN_WINDOW_H_ #define OSKAR_MAIN_WINDOW_H_ #include <QtWidgets/QMainWindow> #include <QtCore/QString> #include <QtCore/QHash> class QComboBox; class QLineEdit; class QWidget; class QNetworkAccessManager; class QNetworkReply; namespace oskar { class SettingsModel; class SettingsView; class SettingsTree; class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget* parent = 0); ~MainWindow(); protected: void closeEvent(QCloseEvent* event); public slots: void clear(); void open(QString filename = QString()); void save(QString filename = QString()); private slots: void about(); void appChanged(QString text = QString()); void setAppDir(bool hint = false); void changeDir(); void checkForUpdate(); void cudaInfo(); void helpDoc(); void processNetworkReply(QNetworkReply*); void runButton(); private: void notFound(const QString& app_name); void run(QString app, const QStringList& args = QStringList(), bool allow_auto_close = true); void setApp(const QString& app, bool refresh = true); private: bool checked_update_; QString title_; QLineEdit* current_dir_label_; QLineEdit* filter_; QComboBox* selector_; SettingsTree* settings_; SettingsView* view_; SettingsModel* model_; QHash<QString, QString> files_; QString current_dir_; QString current_app_; QString current_app_version_; QString app_dir_; QNetworkAccessManager* net_; QString version_url_; }; } /* namespace oskar */ #endif /* OSKAR_MAIN_WINDOW_H_ */
davepallot/OSKAR
oskar/log/oskar_log.h
/* * Copyright (c) 2013-2014, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_LOG_H_ #define OSKAR_LOG_H_ /** * @file oskar_log.h */ /* Public interface. */ #ifdef __cplusplus extern "C" { #endif struct oskar_Log; #ifndef OSKAR_LOG_TYPEDEF_ #define OSKAR_LOG_TYPEDEF_ typedef struct oskar_Log oskar_Log; #endif /* OSKAR_LOG_TYPEDEF_ */ #ifdef __cplusplus } #endif #define OSKAR_LOG_DEFAULT_PRIORITY 3 /* 3 = OSKAR_LOG_STATUS (or code 'S') */ #define OSKAR_LOG_DEFAULT_VALUE_WIDTH 40 /* Default width for value log entries */ enum OSKAR_LOG_SPECIAL_DEPTH { OSKAR_LOG_NO_LIST_MARKER = -1, OSKAR_LOG_INFO_PREFIX = -101, OSKAR_LOG_SECTION = -102, OSKAR_LOG_LINE = -1000 }; /* Enum describing the logging level. * The logging level determines the maximum verbosity of the log * ie. settings the level to DEBUG will print all logs, and setting it WARN * will only print warning logs */ enum OSKAR_LOG_PRIORITY { OSKAR_LOG_NONE = -1, OSKAR_LOG_ERROR = 0, /* == 'E' */ OSKAR_LOG_WARNING = 1, /* == 'W' */ OSKAR_LOG_MESSAGE = 2, /* == 'M' */ OSKAR_LOG_STATUS = 3, /* == 'S' */ OSKAR_LOG_DEBUG = 4 /* == 'D' */ }; #include <log/oskar_log_accessors.h> #include <log/oskar_log_create.h> #include <log/oskar_log_error.h> #include <log/oskar_log_file_data.h> #include <log/oskar_log_file_exists.h> #include <log/oskar_log_free.h> #include <log/oskar_log_line.h> #include <log/oskar_log_message.h> #include <log/oskar_log_section.h> #include <log/oskar_log_system_clock_data.h> #include <log/oskar_log_system_clock_string.h> #include <log/oskar_log_value.h> #include <log/oskar_log_warning.h> #endif /* OSKAR_LOG_H_ */
davepallot/OSKAR
oskar/telescope/station/element/src/oskar_element_accessors.c
<filename>oskar/telescope/station/element/src/oskar_element_accessors.c<gh_stars>1-10 /* * Copyright (c) 2013-2014, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "telescope/station/element/private_element.h" #include "telescope/station/element/oskar_element.h" #include <string.h> #ifdef __cplusplus extern "C" { #endif int oskar_element_precision(const oskar_Element* data) { return data->precision; } int oskar_element_mem_location(const oskar_Element* data) { return data->mem_location; } int oskar_element_has_x_spline_data(const oskar_Element* data) { return (data->num_freq > 0) && ( /* Short-circuit, so will be safe. */ oskar_splines_have_coeffs(data->x_h_re[0]) || oskar_splines_have_coeffs(data->x_h_im[0]) || oskar_splines_have_coeffs(data->x_v_re[0]) || oskar_splines_have_coeffs(data->x_v_im[0])); } int oskar_element_has_y_spline_data(const oskar_Element* data) { return (data->num_freq > 0) && ( /* Short-circuit, so will be safe. */ oskar_splines_have_coeffs(data->y_h_re[0]) || oskar_splines_have_coeffs(data->y_h_im[0]) || oskar_splines_have_coeffs(data->y_v_re[0]) || oskar_splines_have_coeffs(data->y_v_im[0])); } int oskar_element_has_scalar_spline_data(const oskar_Element* data) { return (data->num_freq > 0) && ( /* Short-circuit, so will be safe. */ oskar_splines_have_coeffs(data->scalar_re[0]) || oskar_splines_have_coeffs(data->scalar_im[0])); } int oskar_element_num_freq(const oskar_Element* data) { return data->num_freq; } const double* oskar_element_freqs_hz_const(const oskar_Element* data) { return data->freqs_hz; } int oskar_element_type(const oskar_Element* data) { return data->element_type; } int oskar_element_taper_type(const oskar_Element* data) { return data->taper_type; } double oskar_element_cosine_power(const oskar_Element* data) { return data->cosine_power; } double oskar_element_gaussian_fwhm_rad(const oskar_Element* data) { return data->gaussian_fwhm_rad; } double oskar_element_dipole_length(const oskar_Element* data) { return data->dipole_length; } int oskar_element_dipole_length_units(const oskar_Element* data) { return data->dipole_length_units; } oskar_Mem* oskar_element_x_filename(oskar_Element* data, int freq_id) { return data->filename_x[freq_id]; } const oskar_Mem* oskar_element_x_filename_const(const oskar_Element* data, int freq_id) { return data->filename_x[freq_id]; } oskar_Mem* oskar_element_y_filename(oskar_Element* data, int freq_id) { return data->filename_y[freq_id]; } const oskar_Mem* oskar_element_y_filename_const(const oskar_Element* data, int freq_id) { return data->filename_y[freq_id]; } oskar_Mem* oskar_element_scalar_filename(oskar_Element* data, int freq_id) { return data->filename_scalar[freq_id]; } const oskar_Mem* oskar_element_scalar_filename_const(const oskar_Element* data, int freq_id) { return data->filename_scalar[freq_id]; } oskar_Splines* oskar_element_x_h_re(oskar_Element* data, int freq_id) { if (freq_id >= data->num_freq) return 0; return data->x_h_re[freq_id]; } const oskar_Splines* oskar_element_x_h_re_const(const oskar_Element* data, int freq_id) { if (freq_id >= data->num_freq) return 0; return data->x_h_re[freq_id]; } oskar_Splines* oskar_element_x_h_im(oskar_Element* data, int freq_id) { if (freq_id >= data->num_freq) return 0; return data->x_h_im[freq_id]; } const oskar_Splines* oskar_element_x_h_im_const(const oskar_Element* data, int freq_id) { if (freq_id >= data->num_freq) return 0; return data->x_h_im[freq_id]; } oskar_Splines* oskar_element_x_v_re(oskar_Element* data, int freq_id) { if (freq_id >= data->num_freq) return 0; return data->x_v_re[freq_id]; } const oskar_Splines* oskar_element_x_v_re_const(const oskar_Element* data, int freq_id) { if (freq_id >= data->num_freq) return 0; return data->x_v_re[freq_id]; } oskar_Splines* oskar_element_x_v_im(oskar_Element* data, int freq_id) { if (freq_id >= data->num_freq) return 0; return data->x_v_im[freq_id]; } const oskar_Splines* oskar_element_x_v_im_const(const oskar_Element* data, int freq_id) { if (freq_id >= data->num_freq) return 0; return data->x_v_im[freq_id]; } oskar_Splines* oskar_element_y_h_re(oskar_Element* data, int freq_id) { if (freq_id >= data->num_freq) return 0; return data->y_h_re[freq_id]; } const oskar_Splines* oskar_element_y_h_re_const(const oskar_Element* data, int freq_id) { if (freq_id >= data->num_freq) return 0; return data->y_h_re[freq_id]; } oskar_Splines* oskar_element_y_h_im(oskar_Element* data, int freq_id) { if (freq_id >= data->num_freq) return 0; return data->y_h_im[freq_id]; } const oskar_Splines* oskar_element_y_h_im_const(const oskar_Element* data, int freq_id) { if (freq_id >= data->num_freq) return 0; return data->y_h_im[freq_id]; } oskar_Splines* oskar_element_y_v_re(oskar_Element* data, int freq_id) { if (freq_id >= data->num_freq) return 0; return data->y_v_re[freq_id]; } const oskar_Splines* oskar_element_y_v_re_const(const oskar_Element* data, int freq_id) { if (freq_id >= data->num_freq) return 0; return data->y_v_re[freq_id]; } oskar_Splines* oskar_element_y_v_im(oskar_Element* data, int freq_id) { if (freq_id >= data->num_freq) return 0; return data->y_v_im[freq_id]; } const oskar_Splines* oskar_element_y_v_im_const(const oskar_Element* data, int freq_id) { if (freq_id >= data->num_freq) return 0; return data->y_v_im[freq_id]; } oskar_Splines* oskar_element_scalar_re(oskar_Element* data, int freq_id) { if (freq_id >= data->num_freq) return 0; return data->scalar_re[freq_id]; } const oskar_Splines* oskar_element_scalar_re_const(const oskar_Element* data, int freq_id) { if (freq_id >= data->num_freq) return 0; return data->scalar_re[freq_id]; } oskar_Splines* oskar_element_scalar_im(oskar_Element* data, int freq_id) { if (freq_id >= data->num_freq) return 0; return data->scalar_im[freq_id]; } const oskar_Splines* oskar_element_scalar_im_const(const oskar_Element* data, int freq_id) { if (freq_id >= data->num_freq) return 0; return data->scalar_im[freq_id]; } /* Setters. */ void oskar_element_set_element_type(oskar_Element* data, const char* type, int* status) { if (*status) return; if (!strncmp(type, "D", 1) || !strncmp(type, "d", 1)) data->element_type = OSKAR_ELEMENT_TYPE_DIPOLE; else if (!strncmp(type, "G", 1) || !strncmp(type, "g", 1)) data->element_type = OSKAR_ELEMENT_TYPE_GEOMETRIC_DIPOLE; else if (!strncmp(type, "I", 1) || !strncmp(type, "i", 1)) data->element_type = OSKAR_ELEMENT_TYPE_ISOTROPIC; else *status = OSKAR_ERR_INVALID_ARGUMENT; } void oskar_element_set_taper_type(oskar_Element* data, const char* type, int* status) { if (*status) return; if (!strncmp(type, "N", 1) || !strncmp(type, "n", 1)) data->taper_type = OSKAR_ELEMENT_TAPER_NONE; else if (!strncmp(type, "C", 1) || !strncmp(type, "c", 1)) data->taper_type = OSKAR_ELEMENT_TAPER_COSINE; else if (!strncmp(type, "G", 1) || !strncmp(type, "g", 1)) data->taper_type = OSKAR_ELEMENT_TAPER_GAUSSIAN; else *status = OSKAR_ERR_INVALID_ARGUMENT; } void oskar_element_set_gaussian_fwhm_rad(oskar_Element* data, double value) { data->gaussian_fwhm_rad = value; } void oskar_element_set_cosine_power(oskar_Element* data, double value) { data->cosine_power = value; } void oskar_element_set_dipole_length(oskar_Element* data, double value, const char* units, int* status) { if (*status) return; if (!strncmp(units, "W", 1) || !strncmp(units, "w", 1)) data->dipole_length_units = OSKAR_WAVELENGTHS; else if (!strncmp(units, "M", 1) || !strncmp(units, "m", 1)) data->dipole_length_units = OSKAR_METRES; else { *status = OSKAR_ERR_INVALID_ARGUMENT; return; } data->dipole_length = value; } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/imager/src/private_imager_read_data.c
/* * Copyright (c) 2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "imager/private_imager.h" #include "imager/private_imager_read_data.h" #include "imager/oskar_imager.h" #include "binary/oskar_binary.h" #include "math/oskar_cmath.h" #include "mem/oskar_binary_read_mem.h" #include "ms/oskar_measurement_set.h" #include "vis/oskar_vis_block.h" #include "vis/oskar_vis_header.h" #include "utility/oskar_timer.h" #include <float.h> #include <math.h> #include <stdlib.h> #include <string.h> #ifdef __cplusplus extern "C" { #endif void oskar_imager_read_data_ms(oskar_Imager* h, const char* filename, int i_file, int num_files, int* percent_done, int* percent_next, int* status) { #ifndef OSKAR_NO_MS oskar_MeasurementSet* ms; oskar_Mem *uvw, *u, *v, *w, *data, *weight, *time_centroid; int num_channels, num_pols, num_stations, type; size_t num_baselines, start_row, num_rows; double *uvw_, *u_, *v_, *w_; if (*status) return; /* Read the header. */ ms = oskar_ms_open(filename); if (!ms) { *status = OSKAR_ERR_FILE_IO; return; } num_rows = (size_t) oskar_ms_num_rows(ms); num_stations = (int) oskar_ms_num_stations(ms); num_baselines = num_stations * (num_stations - 1) / 2; num_pols = (int) oskar_ms_num_pols(ms); num_channels = (int) oskar_ms_num_channels(ms); /* Set visibility meta-data. */ oskar_imager_set_vis_frequency(h, oskar_ms_freq_start_hz(ms), oskar_ms_freq_inc_hz(ms), num_channels); oskar_imager_set_vis_phase_centre(h, oskar_ms_phase_centre_ra_rad(ms) * 180/M_PI, oskar_ms_phase_centre_dec_rad(ms) * 180/M_PI); /* Create arrays. */ uvw = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, 3 * num_baselines, status); u = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, num_baselines, status); v = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, num_baselines, status); w = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, num_baselines, status); weight = oskar_mem_create(OSKAR_SINGLE, OSKAR_CPU, num_baselines * num_pols, status); time_centroid = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, num_baselines, status); uvw_ = oskar_mem_double(uvw, status); u_ = oskar_mem_double(u, status); v_ = oskar_mem_double(v, status); w_ = oskar_mem_double(w, status); type = OSKAR_SINGLE | OSKAR_COMPLEX; if (num_pols == 4) type |= OSKAR_MATRIX; data = oskar_mem_create(type, OSKAR_CPU, num_baselines * num_channels, status); /* Loop over visibility blocks. */ for (start_row = 0; start_row < num_rows; start_row += num_baselines) { size_t allocated, required, block_size, i; if (*status) break; /* Read rows from Measurement Set. */ oskar_timer_resume(h->tmr_read); block_size = num_rows - start_row; if (block_size > num_baselines) block_size = num_baselines; allocated = oskar_mem_length(uvw) * oskar_mem_element_size(oskar_mem_type(uvw)); oskar_ms_read_column(ms, "UVW", start_row, block_size, allocated, oskar_mem_void(uvw), &required, status); allocated = oskar_mem_length(weight) * oskar_mem_element_size(oskar_mem_type(weight)); oskar_ms_read_column(ms, "WEIGHT", start_row, block_size, allocated, oskar_mem_void(weight), &required, status); allocated = oskar_mem_length(time_centroid) * oskar_mem_element_size(oskar_mem_type(time_centroid)); oskar_ms_read_column(ms, "TIME_CENTROID", start_row, block_size, allocated, oskar_mem_void(time_centroid), &required, status); allocated = oskar_mem_length(data) * oskar_mem_element_size(oskar_mem_type(data)); oskar_ms_read_column(ms, h->ms_column, start_row, block_size, allocated, oskar_mem_void(data), &required, status); if (*status) break; /* Split up baseline coordinates. */ for (i = 0; i < block_size; ++i) { u_[i] = uvw_[3*i + 0]; v_[i] = uvw_[3*i + 1]; w_[i] = uvw_[3*i + 2]; } /* Update the imager with the data. */ oskar_timer_pause(h->tmr_read); oskar_imager_update(h, block_size, 0, num_channels - 1, num_pols, u, v, w, data, weight, time_centroid, status); *percent_done = (int) round(100.0 * ( (start_row + block_size) / (double)(num_rows * num_files) + i_file / (double)num_files)); if (h->log && percent_next && *percent_done >= *percent_next) { oskar_log_message(h->log, 'S', -2, "%3d%% ...", *percent_done); *percent_next = 10 + 10 * (*percent_done / 10); } } oskar_mem_free(uvw, status); oskar_mem_free(u, status); oskar_mem_free(v, status); oskar_mem_free(w, status); oskar_mem_free(data, status); oskar_mem_free(weight, status); oskar_mem_free(time_centroid, status); oskar_ms_close(ms); #else (void) filename; (void) i_file; (void) num_files; (void) percent_done; (void) percent_next; oskar_log_error(h->log, "OSKAR was compiled without Measurement Set support."); *status = OSKAR_ERR_FUNCTION_NOT_AVAILABLE; #endif } void oskar_imager_read_data_vis(oskar_Imager* h, const char* filename, int i_file, int num_files, int* percent_done, int* percent_next, int* status) { oskar_Binary* vis_file; oskar_VisBlock* block; oskar_VisHeader* header; oskar_Mem *weight, *time_centroid, *time_slice, *scratch = 0, *ptr; int max_times_per_block, tags_per_block, i_block, num_blocks; int num_times_tot, num_channels_tot, num_stations, num_baselines, num_pols; double time_start_mjd, time_inc_sec; if (*status) return; /* Read the header. */ vis_file = oskar_binary_create(filename, 'r', status); header = oskar_vis_header_read(vis_file, status); if (*status) { oskar_vis_header_free(header, status); oskar_binary_free(vis_file); return; } max_times_per_block = oskar_vis_header_max_times_per_block(header); tags_per_block = oskar_vis_header_num_tags_per_block(header); num_times_tot = oskar_vis_header_num_times_total(header); num_channels_tot = oskar_vis_header_num_channels_total(header); num_stations = oskar_vis_header_num_stations(header); num_baselines = num_stations * (num_stations - 1) / 2; num_pols = oskar_type_is_matrix(oskar_vis_header_amp_type(header)) ? 4 : 1; num_blocks = (num_times_tot + max_times_per_block - 1) / max_times_per_block; time_start_mjd = oskar_vis_header_time_start_mjd_utc(header) * 86400.0; time_inc_sec = oskar_vis_header_time_inc_sec(header); /* Set visibility meta-data. */ oskar_imager_set_vis_frequency(h, oskar_vis_header_freq_start_hz(header), oskar_vis_header_freq_inc_hz(header), num_channels_tot); oskar_imager_set_vis_phase_centre(h, oskar_vis_header_phase_centre_ra_deg(header), oskar_vis_header_phase_centre_dec_deg(header)); /* Create scratch arrays. Weights are all 1. */ time_centroid = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, num_baselines * max_times_per_block, status); time_slice = oskar_mem_create_alias(0, 0, 0, status); weight = oskar_mem_create(h->imager_prec, OSKAR_CPU, num_baselines * num_pols * max_times_per_block, status); oskar_mem_set_value_real(weight, 1.0, 0, 0, status); if (num_channels_tot > 1) scratch = oskar_mem_create(oskar_vis_header_amp_type(header), OSKAR_CPU, num_baselines * num_channels_tot * max_times_per_block, status); /* Loop over visibility blocks. */ block = oskar_vis_block_create_from_header(OSKAR_CPU, header, status); for (i_block = 0; i_block < num_blocks; ++i_block) { int t, num_times, num_channels, start_time, start_chan, end_chan; size_t num_rows; if (*status) break; /* Read the visibility data. */ oskar_timer_resume(h->tmr_read); oskar_binary_set_query_search_start(vis_file, i_block * tags_per_block, status); oskar_vis_block_read(block, header, vis_file, i_block, status); start_time = oskar_vis_block_start_time_index(block); start_chan = oskar_vis_block_start_channel_index(block); num_times = oskar_vis_block_num_times(block); num_channels = oskar_vis_block_num_channels(block); num_rows = num_times * num_baselines; end_chan = start_chan + num_channels - 1; /* Fill in the time centroid values. */ for (t = 0; t < num_times; ++t) { oskar_mem_set_alias(time_slice, time_centroid, t * num_baselines, num_baselines, status); oskar_mem_set_value_real(time_slice, time_start_mjd + (start_time + t + 0.5) * time_inc_sec, 0, num_baselines, status); } /* Swap baseline and channel dimensions. */ ptr = oskar_vis_block_cross_correlations(block); #define SWAP_LOOP \ for (t = 0; t < num_times; ++t) \ for (c = 0; c < num_channels; ++c) \ for (b = 0; b < num_baselines; ++b) \ for (p = 0; p < num_pols; ++p) \ { \ k = (num_pols * (num_baselines * \ (num_channels * t + c) + b) + p) << 1; \ l = (num_pols * (num_channels * \ (num_baselines * t + b) + c) + p) << 1; \ out[l] = in[k]; \ out[l + 1] = in[k + 1]; \ } if (num_channels != 1) { int b, c, p; size_t k, l; if (oskar_mem_precision(ptr) == OSKAR_SINGLE) { float *in, *out; in = oskar_mem_float(ptr, status); out = oskar_mem_float(scratch, status); SWAP_LOOP } else { double *in, *out; in = oskar_mem_double(ptr, status); out = oskar_mem_double(scratch, status); SWAP_LOOP } ptr = scratch; } #undef SWAP_LOOP /* Update the imager with the data. */ oskar_timer_pause(h->tmr_read); oskar_imager_update(h, num_rows, start_chan, end_chan, num_pols, oskar_vis_block_baseline_uu_metres(block), oskar_vis_block_baseline_vv_metres(block), oskar_vis_block_baseline_ww_metres(block), ptr, weight, time_centroid, status); *percent_done = (int) round(100.0 * ( (i_block + 1) / (double)(num_blocks * num_files) + i_file / (double)num_files)); if (h->log && percent_next && *percent_done >= *percent_next) { oskar_log_message(h->log, 'S', -2, "%3d%% ...", *percent_done); *percent_next = 10 + 10 * (*percent_done / 10); } } oskar_mem_free(scratch, status); oskar_mem_free(weight, status); oskar_mem_free(time_centroid, status); oskar_mem_free(time_slice, status); oskar_vis_block_free(block, status); oskar_vis_header_free(header, status); oskar_binary_free(vis_file); } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/convert/src/oskar_convert_apparent_ha_dec_to_enu_directions.c
/* * Copyright (c) 2013-2014, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "convert/oskar_convert_apparent_ha_dec_to_enu_directions.h" #include <math.h> #ifdef __cplusplus extern "C" { #endif /* Single precision. */ void oskar_convert_apparent_ha_dec_to_enu_directions_f(int n, const float* ha, const float* dec, float lat, float* x, float* y, float* z) { float sinLat, cosLat; int i; /* Compute latitude trig. */ sinLat = sinf(lat); cosLat = cosf(lat); /* Loop over positions. */ for (i = 0; i < n; ++i) { float sh, sd, sinHA, sinDec, cosHA, cosDec, t, X1, Y2; /* Load local equatorial coordinates. */ sh = ha[i]; sd = dec[i]; /* Find direction cosines. */ sinHA = sinf(sh); sinDec = sinf(sd); cosHA = cosf(sh); cosDec = cosf(sd); t = cosDec * cosHA; X1 = cosLat * sinDec - sinLat * t; Y2 = sinLat * sinDec + cosLat * t; t = -cosDec * sinHA; /* Store source direction cosines. */ x[i] = t; /* Horizontal x-component. */ y[i] = X1; /* Horizontal y-component. */ z[i] = Y2; /* Horizontal z-component. */ } } /* Double precision. */ void oskar_convert_apparent_ha_dec_to_enu_directions_d(int n, const double* ha, const double* dec, double lat, double* x, double* y, double* z) { double sinLat, cosLat; int i; /* Compute latitude trig. */ sinLat = sin(lat); cosLat = cos(lat); /* Loop over positions. */ for (i = 0; i < n; ++i) { double sh, sd, sinHA, sinDec, cosHA, cosDec, t, X1, Y2; /* Load local equatorial coordinates. */ sh = ha[i]; sd = dec[i]; /* Find direction cosines. */ sinHA = sin(sh); sinDec = sin(sd); cosHA = cos(sh); cosDec = cos(sd); t = cosDec * cosHA; X1 = cosLat * sinDec - sinLat * t; Y2 = sinLat * sinDec + cosLat * t; t = -cosDec * sinHA; /* Store source direction cosines. */ x[i] = t; /* Horizontal x-component. */ y[i] = X1; /* Horizontal y-component. */ z[i] = Y2; /* Horizontal z-component. */ } } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/telescope/station/src/oskar_station_create.c
<filename>oskar/telescope/station/src/oskar_station_create.c /* * Copyright (c) 2011-2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "telescope/station/private_station.h" #include "telescope/station/oskar_station.h" #include "math/oskar_cmath.h" #include <string.h> #ifdef __cplusplus extern "C" { #endif oskar_Station* oskar_station_create(int type, int location, int num_elements, int* status) { oskar_Station* model; /* Check the type and location. */ if (type != OSKAR_SINGLE && type != OSKAR_DOUBLE) { *status = OSKAR_ERR_BAD_DATA_TYPE; return 0; } /* Allocate and initialise a station model structure. */ model = (oskar_Station*) malloc(sizeof(oskar_Station)); if (!model) { *status = OSKAR_ERR_MEMORY_ALLOC_FAILURE; return 0; } /* Initialise station meta data. */ model->unique_id = 0; model->precision = type; model->mem_location = location; /* Initialise the memory. */ model->element_true_x_enu_metres = oskar_mem_create(type, location, num_elements, status); model->element_true_y_enu_metres = oskar_mem_create(type, location, num_elements, status); model->element_true_z_enu_metres = oskar_mem_create(type, location, num_elements, status); model->element_measured_x_enu_metres = oskar_mem_create(type, location, num_elements, status); model->element_measured_y_enu_metres = oskar_mem_create(type, location, num_elements, status); model->element_measured_z_enu_metres = oskar_mem_create(type, location, num_elements, status); model->element_weight = oskar_mem_create(type | OSKAR_COMPLEX, location, num_elements, status); model->element_gain = oskar_mem_create(type, location, num_elements, status); model->element_gain_error = oskar_mem_create(type, location, num_elements, status); model->element_phase_offset_rad = oskar_mem_create(type, location, num_elements, status); model->element_phase_error_rad = oskar_mem_create(type, location, num_elements, status); model->element_x_alpha_cpu = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, num_elements, status); model->element_x_beta_cpu = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, num_elements, status); model->element_x_gamma_cpu = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, num_elements, status); model->element_y_alpha_cpu = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, num_elements, status); model->element_y_beta_cpu = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, num_elements, status); model->element_y_gamma_cpu = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, num_elements, status); model->element_types = oskar_mem_create(OSKAR_INT, location, num_elements, status); model->element_types_cpu = oskar_mem_create(OSKAR_INT, OSKAR_CPU, num_elements, status); model->element_mount_types_cpu = oskar_mem_create(OSKAR_CHAR, OSKAR_CPU, num_elements, status); model->permitted_beam_az_rad = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, 0, status); model->permitted_beam_el_rad = oskar_mem_create(OSKAR_DOUBLE, OSKAR_CPU, 0, status); /* Initialise common data. */ model->station_type = OSKAR_STATION_TYPE_AA; model->normalise_final_beam = OSKAR_TRUE; model->lon_rad = 0.0; model->lat_rad = 0.0; model->alt_metres = 0.0; model->pm_x_rad = 0.0; model->pm_y_rad = 0.0; model->beam_lon_rad = 0.0; model->beam_lat_rad = 0.0; model->beam_coord_type = OSKAR_SPHERICAL_TYPE_EQUATORIAL; model->noise_freq_hz = oskar_mem_create(type, OSKAR_CPU, 0, status); model->noise_rms_jy = oskar_mem_create(type, OSKAR_CPU, 0, status); /* Initialise Gaussian beam station data. */ model->gaussian_beam_fwhm_rad = 0.0; model->gaussian_beam_reference_freq_hz = 0.0; /* Initialise aperture array data. */ model->identical_children = OSKAR_FALSE; model->num_elements = num_elements; model->num_element_types = 0; model->normalise_array_pattern = OSKAR_FALSE; model->enable_array_pattern = OSKAR_TRUE; model->common_element_orientation = OSKAR_TRUE; model->array_is_3d = OSKAR_FALSE; model->apply_element_errors = OSKAR_FALSE; model->apply_element_weight = OSKAR_FALSE; model->seed_time_variable_errors = 1; model->child = 0; model->element = 0; model->num_permitted_beams = 0; if (num_elements > 0) memset(oskar_mem_void(model->element_mount_types_cpu), 'F', num_elements); /* Return pointer to station model. */ return model; } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/utility/private_cuda_info.h
<reponame>davepallot/OSKAR<gh_stars>1-10 /* * Copyright (c) 2012-2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_PRIVATE_CUDA_INFO_H_ #define OSKAR_PRIVATE_CUDA_INFO_H_ #include <stddef.h> #ifdef __cplusplus extern "C" { #endif struct oskar_CudaDeviceInfo { char name[256]; /* String holding device name. */ union { struct { int major; /* Compute capability, major version. */ int minor; /* Compute capability, minor version. */ } capability; int version[2]; } compute; int supports_double; /* True if device supports double precision. */ size_t global_memory_size; /* Total size in kiB. */ size_t free_memory; /* Free memory in kiB. */ int num_multiprocessors; /* Number of multiprocessors. */ int num_cores; /* Number of CUDA cores. */ int gpu_clock; /* GPU clock speed in kHz. */ int memory_clock; /* Memory clock speed in kHz. */ int memory_bus_width; /* Memory bus width in bits. */ int level_2_cache_size; /* Cache size in bytes. */ size_t shared_memory_size; /* Shared memory per block in bytes. */ int num_registers; /* Number of registers per block. */ int warp_size; /* Warp size. */ int max_threads_per_block; /* Maximum number of threads per block. */ int max_threads_dim[3]; /* Maximum threads per dimension. */ int max_grid_size[3]; /* Maximum grid size per dimension. */ }; typedef struct oskar_CudaDeviceInfo oskar_CudaDeviceInfo; struct oskar_CudaInfo { int num_devices; /* Number of installed CUDA devices. */ int driver_version; /* CUDA driver version. */ int runtime_version; /* CUDA runtime version. */ oskar_CudaDeviceInfo* device; /* Array of device info structures. */ }; #ifndef OSKAR_CUDA_INFO_TYPEDEF_ #define OSKAR_CUDA_INFO_TYPEDEF_ typedef struct oskar_CudaInfo oskar_CudaInfo; #endif /* OSKAR_CUDA_INFO_TYPEDEF_ */ #ifdef __cplusplus } #endif #endif /* OSKAR_PRIVATE_CUDA_INFO_H_ */
davepallot/OSKAR
oskar/convert/src/oskar_convert_cirs_ra_dec_to_enu_directions.c
<filename>oskar/convert/src/oskar_convert_cirs_ra_dec_to_enu_directions.c /* * Copyright (c) 2014, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "convert/oskar_convert_cirs_ra_dec_to_enu_directions.h" #include "convert/private_convert_cirs_relative_directions_to_enu_directions_inline.h" #include "convert/private_evaluate_cirs_observed_parameters.h" #include <math.h> #ifdef __cplusplus extern "C" { #endif /* Single precision. */ void oskar_convert_cirs_ra_dec_to_enu_directions_f(int num_points, const float* ra_rad, const float* dec_rad, float lon_rad, float lat_rad, float era_rad, float pm_x_rad, float pm_y_rad, float diurnal_aberration, float* x, float* y, float* z) { int i; double sin_lat, cos_lat, sin_ha0, cos_ha0, sin_dec0, cos_dec0; double local_pm_x, local_pm_y; /* Calculate common transform parameters. */ oskar_evaluate_cirs_observed_parameters(lon_rad, lat_rad, era_rad, 0.0, 0.0, pm_x_rad, pm_y_rad, &sin_lat, &cos_lat, &sin_ha0, &cos_ha0, &sin_dec0, &cos_dec0, &local_pm_x, &local_pm_y); /* Loop over positions. */ for (i = 0; i < num_points; ++i) { float ra, dec, cos_dec, l, m, n; /* Get CIRS RA, Dec. */ ra = ra_rad[i]; dec = dec_rad[i]; /* Convert to direction cosines (relative to RA=0, Dec=0). */ cos_dec = cosf(dec); l = cos_dec * sinf(ra); m = sinf(dec); n = cos_dec * cosf(ra); /* Convert CIRS relative directions to ENU directions. */ oskar_convert_cirs_relative_directions_to_enu_directions_inline_f( l, m, n, (float)sin_lat, (float)cos_lat, (float)sin_ha0, (float)cos_ha0, (float)sin_dec0, (float)cos_dec0, (float)local_pm_x, (float)local_pm_y, diurnal_aberration, &x[i], &y[i], &z[i]); } } /* Double precision. */ void oskar_convert_cirs_ra_dec_to_enu_directions_d(int num_points, const double* ra_rad, const double* dec_rad, double lon_rad, double lat_rad, double era_rad, double pm_x_rad, double pm_y_rad, double diurnal_aberration, double* x, double* y, double* z) { int i; double sin_lat, cos_lat, sin_ha0, cos_ha0, sin_dec0, cos_dec0; double local_pm_x, local_pm_y; /* Calculate common transform parameters. */ oskar_evaluate_cirs_observed_parameters(lon_rad, lat_rad, era_rad, 0.0, 0.0, pm_x_rad, pm_y_rad, &sin_lat, &cos_lat, &sin_ha0, &cos_ha0, &sin_dec0, &cos_dec0, &local_pm_x, &local_pm_y); /* Loop over positions. */ for (i = 0; i < num_points; ++i) { double ra, dec, cos_dec, l, m, n; /* Get CIRS RA, Dec. */ ra = ra_rad[i]; dec = dec_rad[i]; /* Convert to direction cosines (relative to RA=0, Dec=0). */ cos_dec = cos(dec); l = cos_dec * sin(ra); m = sin(dec); n = cos_dec * cos(ra); /* Convert CIRS relative directions to ENU directions. */ oskar_convert_cirs_relative_directions_to_enu_directions_inline_d( l, m, n, sin_lat, cos_lat, sin_ha0, cos_ha0, sin_dec0, cos_dec0, local_pm_x, local_pm_y, diurnal_aberration, &x[i], &y[i], &z[i]); } } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/vis/src/oskar_vis_header_read.c
/* * Copyright (c) 2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "vis/private_vis_header.h" #include "vis/oskar_vis_header.h" #include "mem/oskar_binary_read_mem.h" #include <math.h> #ifdef __cplusplus extern "C" { #endif oskar_VisHeader* oskar_vis_header_read(oskar_Binary* h, int* status) { /* Visibility metadata. */ int num_channels_total = 0, num_times_total = 0, num_stations = 0; int max_channels_per_block = 0, max_times_per_block = 0, tag_error = 0; int amp_type = 0, coord_precision = 0; int write_crosscorr = 0, write_autocorr = 0; unsigned char grp = OSKAR_TAG_GROUP_VIS_HEADER; oskar_VisHeader* vis = 0; /* Check if safe to proceed. */ if (*status) return 0; /* Read essential metadata. */ oskar_binary_read_int(h, grp, OSKAR_VIS_HEADER_TAG_WRITE_AUTO_CORRELATIONS, 0, &write_autocorr, status); oskar_binary_read_int(h, grp, OSKAR_VIS_HEADER_TAG_WRITE_CROSS_CORRELATIONS, 0, &write_crosscorr, status); oskar_binary_read_int(h, grp, OSKAR_VIS_HEADER_TAG_AMP_TYPE, 0, &amp_type, status); oskar_binary_read_int(h, grp, OSKAR_VIS_HEADER_TAG_COORD_PRECISION, 0, &coord_precision, status); oskar_binary_read_int(h, grp, OSKAR_VIS_HEADER_TAG_MAX_TIMES_PER_BLOCK, 0, &max_times_per_block, status); oskar_binary_read_int(h, grp, OSKAR_VIS_HEADER_TAG_NUM_TIMES_TOTAL, 0, &num_times_total, status); oskar_binary_read_int(h, grp, OSKAR_VIS_HEADER_TAG_MAX_CHANNELS_PER_BLOCK, 0, &max_channels_per_block, status); oskar_binary_read_int(h, grp, OSKAR_VIS_HEADER_TAG_NUM_CHANNELS_TOTAL, 0, &num_channels_total, status); oskar_binary_read_int(h, grp, OSKAR_VIS_HEADER_TAG_NUM_STATIONS, 0, &num_stations, status); /* Check if safe to proceed. */ if (*status) return 0; /* Create the visibility header. */ vis = oskar_vis_header_create(amp_type, coord_precision, max_times_per_block, num_times_total, max_channels_per_block, num_channels_total, num_stations, write_autocorr, write_crosscorr, status); if (*status) return vis; /* Read the number of tags per block. */ oskar_binary_read_int(h, grp, OSKAR_VIS_HEADER_TAG_NUM_TAGS_PER_BLOCK, 0, &vis->num_tags_per_block, status); /* Optionally read the settings data (ignore the error code). */ tag_error = 0; oskar_binary_read_mem(h, vis->settings, OSKAR_TAG_GROUP_SETTINGS, OSKAR_TAG_SETTINGS, 0, &tag_error); /* Read the telescope model path. */ oskar_binary_read_mem(h, vis->telescope_path, grp, OSKAR_VIS_HEADER_TAG_TELESCOPE_PATH, 0, status); /* Read other visibility metadata. */ oskar_binary_read_int(h, grp, OSKAR_VIS_HEADER_TAG_POL_TYPE, 0, &vis->pol_type, status); oskar_binary_read_int(h, grp, OSKAR_VIS_HEADER_TAG_PHASE_CENTRE_COORD_TYPE, 0, &vis->phase_centre_type, status); oskar_binary_read(h, OSKAR_DOUBLE, grp, OSKAR_VIS_HEADER_TAG_PHASE_CENTRE_DEG, 0, 2 * sizeof(double), &vis->phase_centre_deg, status); oskar_binary_read_double(h, grp, OSKAR_VIS_HEADER_TAG_FREQ_START_HZ, 0, &vis->freq_start_hz, status); oskar_binary_read_double(h, grp, OSKAR_VIS_HEADER_TAG_FREQ_INC_HZ, 0, &vis->freq_inc_hz, status); oskar_binary_read_double(h, grp, OSKAR_VIS_HEADER_TAG_CHANNEL_BANDWIDTH_HZ, 0, &vis->channel_bandwidth_hz, status); oskar_binary_read_double(h, grp, OSKAR_VIS_HEADER_TAG_TIME_START_MJD_UTC, 0, &vis->time_start_mjd_utc, status); oskar_binary_read_double(h, grp, OSKAR_VIS_HEADER_TAG_TIME_INC_SEC, 0, &vis->time_inc_sec, status); oskar_binary_read_double(h, grp, OSKAR_VIS_HEADER_TAG_TIME_AVERAGE_SEC, 0, &vis->time_average_sec, status); oskar_binary_read_double(h, grp, OSKAR_VIS_HEADER_TAG_TELESCOPE_REF_LON_DEG, 0, &vis->telescope_centre_lon_deg, status); oskar_binary_read_double(h, grp, OSKAR_VIS_HEADER_TAG_TELESCOPE_REF_LAT_DEG, 0, &vis->telescope_centre_lat_deg, status); oskar_binary_read_double(h, grp, OSKAR_VIS_HEADER_TAG_TELESCOPE_REF_ALT_M, 0, &vis->telescope_centre_alt_m, status); /* Read the station coordinates. */ oskar_binary_read_mem(h, vis->station_x_offset_ecef_metres, grp, OSKAR_VIS_HEADER_TAG_STATION_X_OFFSET_ECEF, 0, status); oskar_binary_read_mem(h, vis->station_y_offset_ecef_metres, grp, OSKAR_VIS_HEADER_TAG_STATION_Y_OFFSET_ECEF, 0, status); oskar_binary_read_mem(h, vis->station_z_offset_ecef_metres, grp, OSKAR_VIS_HEADER_TAG_STATION_Z_OFFSET_ECEF, 0, status); /* Return a handle to the new structure. */ return vis; } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/mem/src/oskar_mem_set_element.c
<gh_stars>1-10 /* * Copyright (c) 2014-2016, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifdef OSKAR_HAVE_CUDA #include <cuda_runtime_api.h> #endif #include "mem/oskar_mem.h" #include "mem/private_mem.h" #ifdef __cplusplus extern "C" { #endif void oskar_mem_set_element_real(oskar_Mem* mem, size_t index, double val, int* status) { int precision, location; size_t bound = 0; /* Check if safe to proceed. */ if (*status) return; /* Get the data type and location */ precision = oskar_type_precision(mem->type); location = mem->location; bound = mem->num_elements; if (oskar_mem_is_complex(mem)) bound *= 2; if (oskar_mem_is_matrix(mem)) bound *= 4; if (index >= bound) { *status = OSKAR_ERR_OUT_OF_RANGE; return; } /* Set the data into the array. */ if (location == OSKAR_CPU) { if (precision == OSKAR_DOUBLE) { ((double*)(mem->data))[index] = val; return; } else if (precision == OSKAR_SINGLE) { ((float*)(mem->data))[index] = (float) val; return; } else *status = OSKAR_ERR_BAD_DATA_TYPE; } else if (location == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA if (precision == OSKAR_DOUBLE) { cudaMemcpy((double*)(mem->data) + index, &val, sizeof(double), cudaMemcpyHostToDevice); return; } else if (precision == OSKAR_SINGLE) { float temp; temp = (float) val; cudaMemcpy((float*)(mem->data) + index, &temp, sizeof(float), cudaMemcpyHostToDevice); return; } else *status = OSKAR_ERR_BAD_DATA_TYPE; #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } else *status = OSKAR_ERR_BAD_LOCATION; } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/convert/oskar_convert_cirs_relative_directions_to_enu_directions.h
<reponame>davepallot/OSKAR /* * Copyright (c) 2014, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_CONVERT_CIRS_RELATIVE_DIRECTIONS_TO_ENU_DIRECTIONS_H_ #define OSKAR_CONVERT_CIRS_RELATIVE_DIRECTIONS_TO_ENU_DIRECTIONS_H_ /** * @file oskar_convert_cirs_relative_directions_to_enu_directions.h */ #include <oskar_global.h> #include <mem/oskar_mem.h> #ifdef __cplusplus extern "C" { #endif /** * @brief * Converts CIRS relative direction cosines to ENU direction cosines * (single precision). * * @details * This function converts CIRS vector components relative to * the given right ascension and declination to topocentric east-north-up * vector components for an observer at the given location. * * Allowance is made for polar motion and diurnal aberration. * The magnitude of the diurnal aberration vector can be obtained by calling * oskar_evaluate_diurnal_aberration(). * If not known, these quantities can be set to zero. * * No allowance is made for atmospheric refraction. * * To be rigorous, lon_rad is east longitude + s_prime, the direction to the * terrestial intermediate origin (TIO). This correction is sufficiently small * to be safely neglected for all but the most precise applications. * * This function uses the same algorithm as implemented in the IAU * Standards of Fundamental Astronomy (SOFA) library. * * @param[in] num_points Number of points in all arrays. * @param[in] l CIRS relative directions (l-component). * @param[in] m CIRS relative directions (m-component). * @param[in] n CIRS relative directions (n-component). * @param[in] ra0_rad CIRS reference right ascension, in radians. * @param[in] dec0_rad CIRS reference declination, in radians. * @param[in] lon_rad Observer's longitude, in radians. * @param[in] lat_rad Observer's geodetic latitude, in radians. * @param[in] era_rad Current Earth rotation angle, in radians. * @param[in] pm_x_rad Polar motion, x component, in radians. * @param[in] pm_y_rad Polar motion, y component, in radians. * @param[in] diurnal_aberration Magnitude of diurnal aberration vector. * @param[out] x Output ENU x (east) vector components. * @param[out] y Output ENU y (north) vector components. * @param[out] z Output ENU z (up) vector components. */ OSKAR_EXPORT void oskar_convert_cirs_relative_directions_to_enu_directions_f( int num_points, const float* l, const float* m, const float* n, float ra0_rad, float dec0_rad, float lon_rad, float lat_rad, float era_rad, float pm_x_rad, float pm_y_rad, float diurnal_aberration, float* x, float* y, float* z); /** * @brief * Converts CIRS relative direction cosines to ENU direction cosines * (double precision). * * @details * This function converts CIRS vector components relative to * the given right ascension and declination to topocentric east-north-up * vector components for an observer at the given location. * * Allowance is made for polar motion and diurnal aberration. * The magnitude of the diurnal aberration vector can be obtained by calling * oskar_evaluate_diurnal_aberration(). * If not known, these quantities can be set to zero. * * No allowance is made for atmospheric refraction. * * To be rigorous, lon_rad is east longitude + s_prime, the direction to the * terrestial intermediate origin (TIO). This correction is sufficiently small * to be safely neglected for all but the most precise applications. * * This function uses the same algorithm as implemented in the IAU * Standards of Fundamental Astronomy (SOFA) library. * * @param[in] num_points Number of points in all arrays. * @param[in] l CIRS relative directions (l-component). * @param[in] m CIRS relative directions (m-component). * @param[in] n CIRS relative directions (n-component). * @param[in] ra0_rad CIRS reference right ascension, in radians. * @param[in] dec0_rad CIRS reference declination, in radians. * @param[in] lon_rad Observer's longitude, in radians. * @param[in] lat_rad Observer's geodetic latitude, in radians. * @param[in] era_rad Current Earth rotation angle, in radians. * @param[in] pm_x_rad Polar motion, x component, in radians. * @param[in] pm_y_rad Polar motion, y component, in radians. * @param[in] diurnal_aberration Magnitude of diurnal aberration vector. * @param[out] x Output ENU x (east) vector components. * @param[out] y Output ENU y (north) vector components. * @param[out] z Output ENU z (up) vector components. */ OSKAR_EXPORT void oskar_convert_cirs_relative_directions_to_enu_directions_d( int num_points, const double* l, const double* m, const double* n, double ra0_rad, double dec0_rad, double lon_rad, double lat_rad, double era_rad, double pm_x_rad, double pm_y_rad, double diurnal_aberration, double* x, double* y, double* z); /** * @brief * Converts CIRS relative direction cosines to ENU direction cosines * (wrapper). * * @details * This function converts CIRS vector components relative to * the given right ascension and declination to topocentric east-north-up * vector components for an observer at the given location. * * Allowance is made for polar motion and diurnal aberration. * The magnitude of the diurnal aberration vector can be obtained by calling * oskar_evaluate_diurnal_aberration(). * If not known, these quantities can be set to zero. * * No allowance is made for atmospheric refraction. * * To be rigorous, lon_rad is east longitude + s_prime, the direction to the * terrestial intermediate origin (TIO). This correction is sufficiently small * to be safely neglected for all but the most precise applications. * * This function uses the same algorithm as implemented in the IAU * Standards of Fundamental Astronomy (SOFA) library. * * @param[in] num_points Number of points in all arrays. * @param[in] l CIRS relative directions (l-component). * @param[in] m CIRS relative directions (m-component). * @param[in] n CIRS relative directions (n-component). * @param[in] ra0_rad CIRS reference right ascension, in radians. * @param[in] dec0_rad CIRS reference declination, in radians. * @param[in] lon_rad Observer's longitude, in radians. * @param[in] lat_rad Observer's geodetic latitude, in radians. * @param[in] era_rad Current Earth rotation angle, in radians. * @param[in] pm_x_rad Polar motion, x component, in radians. * @param[in] pm_y_rad Polar motion, y component, in radians. * @param[in] diurnal_aberration Magnitude of diurnal aberration vector. * @param[out] x Output ENU x (east) vector components. * @param[out] y Output ENU y (north) vector components. * @param[out] z Output ENU z (up) vector components. */ OSKAR_EXPORT void oskar_convert_cirs_relative_directions_to_enu_directions(int num_points, const oskar_Mem* l, const oskar_Mem* m, const oskar_Mem* n, double ra0_rad, double dec0_rad, double lon_rad, double lat_rad, double era_rad, double pm_x_rad, double pm_y_rad, double diurnal_aberration, oskar_Mem* x, oskar_Mem* y, oskar_Mem* z, int* status); #ifdef __cplusplus } #endif #endif /* OSKAR_CONVERT_CIRS_RELATIVE_DIRECTIONS_TO_ENU_DIRECTIONS_H_ */
davepallot/OSKAR
oskar/math/src/oskar_dftw.c
/* * Copyright (c) 2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "math/oskar_dftw.h" #include "math/oskar_dftw_c2c_2d_cuda.h" #include "math/oskar_dftw_c2c_2d_omp.h" #include "math/oskar_dftw_c2c_3d_cuda.h" #include "math/oskar_dftw_c2c_3d_omp.h" #include "math/oskar_dftw_m2m_2d_cuda.h" #include "math/oskar_dftw_m2m_2d_omp.h" #include "math/oskar_dftw_m2m_3d_cuda.h" #include "math/oskar_dftw_m2m_3d_omp.h" #include "math/oskar_dftw_o2c_2d_cuda.h" #include "math/oskar_dftw_o2c_2d_omp.h" #include "math/oskar_dftw_o2c_3d_cuda.h" #include "math/oskar_dftw_o2c_3d_omp.h" #include "utility/oskar_cl_utils.h" #include "utility/oskar_device_utils.h" void oskar_dftw( int num_in, double wavenumber, const oskar_Mem* x_in, const oskar_Mem* y_in, const oskar_Mem* z_in, const oskar_Mem* weights_in, int num_out, const oskar_Mem* x_out, const oskar_Mem* y_out, const oskar_Mem* z_out, const oskar_Mem* data, oskar_Mem* output, int* status) { int location, type, is_data, is_dbl, is_matrix, is_3d; if (*status) return; /* Find out what we have. */ location = oskar_mem_location(output); type = oskar_mem_precision(output); is_dbl = type & OSKAR_DOUBLE; is_3d = (z_in != NULL && z_out != NULL); is_data = (data != NULL); is_matrix = oskar_mem_is_matrix(output); if (!oskar_mem_is_complex(output) || !oskar_mem_is_complex(weights_in) || oskar_mem_is_matrix(weights_in)) { *status = OSKAR_ERR_BAD_DATA_TYPE; return; } /* Check type and location consistency. */ if (oskar_mem_location(weights_in) != location || oskar_mem_location(x_in) != location || oskar_mem_location(y_in) != location || oskar_mem_location(x_out) != location || oskar_mem_location(y_out) != location) { *status = OSKAR_ERR_LOCATION_MISMATCH; return; } if (oskar_mem_precision(weights_in) != type || oskar_mem_type(x_in) != type || oskar_mem_type(y_in) != type || oskar_mem_type(x_out) != type || oskar_mem_type(y_out) != type) { *status = OSKAR_ERR_TYPE_MISMATCH; return; } if (is_data) { if (oskar_mem_location(data) != location) { *status = OSKAR_ERR_LOCATION_MISMATCH; return; } if (!oskar_mem_is_complex(data) || oskar_mem_type(data) != oskar_mem_type(output) || oskar_mem_precision(data) != type) { *status = OSKAR_ERR_TYPE_MISMATCH; return; } } if (is_3d) { if (oskar_mem_location(z_in) != location || oskar_mem_location(z_out) != location) { *status = OSKAR_ERR_LOCATION_MISMATCH; return; } if (oskar_mem_type(z_in) != type || oskar_mem_type(z_out) != type) { *status = OSKAR_ERR_TYPE_MISMATCH; return; } } /* Resize output array if needed. */ if ((int)oskar_mem_length(output) < num_out) oskar_mem_realloc(output, (size_t) num_out, status); if (*status) return; /* Switch on location. */ if (location & OSKAR_CL) { #ifdef OSKAR_HAVE_OPENCL cl_device_type dev_type; cl_event event; cl_kernel k; cl_int is_gpu, error, n_in, n_out; cl_uint arg = 0; size_t global_size, local_size; /* Get the appropriate kernel. */ clGetDeviceInfo(oskar_cl_device_id(), CL_DEVICE_TYPE, sizeof(cl_device_type), &dev_type, NULL); is_gpu = dev_type & CL_DEVICE_TYPE_GPU; if (is_data) { if (is_matrix) { if (is_3d) { k = is_gpu ? (is_dbl ? oskar_cl_kernel("dftw_m2m_3d_double") : oskar_cl_kernel("dftw_m2m_3d_float")) : (is_dbl ? oskar_cl_kernel("dftw_m2m_3d_cpu_double") : oskar_cl_kernel("dftw_m2m_3d_cpu_float")); } else { k = is_gpu ? (is_dbl ? oskar_cl_kernel("dftw_m2m_2d_double") : oskar_cl_kernel("dftw_m2m_2d_float")) : (is_dbl ? oskar_cl_kernel("dftw_m2m_2d_cpu_double") : oskar_cl_kernel("dftw_m2m_2d_cpu_float")); } } else { if (is_3d) { k = is_gpu ? (is_dbl ? oskar_cl_kernel("dftw_c2c_3d_double") : oskar_cl_kernel("dftw_c2c_3d_float")) : (is_dbl ? oskar_cl_kernel("dftw_c2c_3d_cpu_double") : oskar_cl_kernel("dftw_c2c_3d_cpu_float")); } else { k = is_gpu ? (is_dbl ? oskar_cl_kernel("dftw_c2c_2d_double") : oskar_cl_kernel("dftw_c2c_2d_float")) : (is_dbl ? oskar_cl_kernel("dftw_c2c_2d_cpu_double") : oskar_cl_kernel("dftw_c2c_2d_cpu_float")); } } } else { if (is_3d) { k = is_gpu ? (is_dbl ? oskar_cl_kernel("dftw_o2c_3d_double") : oskar_cl_kernel("dftw_o2c_3d_float")) : (is_dbl ? oskar_cl_kernel("dftw_o2c_3d_cpu_double") : oskar_cl_kernel("dftw_o2c_3d_cpu_float")); } else { k = is_gpu ? (is_dbl ? oskar_cl_kernel("dftw_o2c_2d_double") : oskar_cl_kernel("dftw_o2c_2d_float")) : (is_dbl ? oskar_cl_kernel("dftw_o2c_2d_cpu_double") : oskar_cl_kernel("dftw_o2c_2d_cpu_float")); } } if (!k) { *status = OSKAR_ERR_FUNCTION_NOT_AVAILABLE; return; } /* Set kernel arguments. */ n_in = (cl_int) num_in; n_out = (cl_int) num_out; error = clSetKernelArg(k, arg++, sizeof(cl_int), &n_in); if (is_dbl) { cl_double w = (cl_double) wavenumber; error |= clSetKernelArg(k, arg++, sizeof(cl_double), &w); } else { cl_float w = (cl_float) wavenumber; error |= clSetKernelArg(k, arg++, sizeof(cl_float), &w); } error |= clSetKernelArg(k, arg++, sizeof(cl_mem), oskar_mem_cl_buffer_const(x_in, status)); error |= clSetKernelArg(k, arg++, sizeof(cl_mem), oskar_mem_cl_buffer_const(y_in, status)); if (is_3d) error |= clSetKernelArg(k, arg++, sizeof(cl_mem), oskar_mem_cl_buffer_const(z_in, status)); error |= clSetKernelArg(k, arg++, sizeof(cl_mem), oskar_mem_cl_buffer_const(weights_in, status)); error |= clSetKernelArg(k, arg++, sizeof(cl_int), &n_out); error |= clSetKernelArg(k, arg++, sizeof(cl_mem), oskar_mem_cl_buffer_const(x_out, status)); error |= clSetKernelArg(k, arg++, sizeof(cl_mem), oskar_mem_cl_buffer_const(y_out, status)); if (is_3d) error |= clSetKernelArg(k, arg++, sizeof(cl_mem), oskar_mem_cl_buffer_const(z_out, status)); if (is_data) error |= clSetKernelArg(k, arg++, sizeof(cl_mem), oskar_mem_cl_buffer_const(data, status)); error |= clSetKernelArg(k, arg++, sizeof(cl_mem), oskar_mem_cl_buffer(output, status)); if (is_gpu) { /* max_in_chunk must be multiple of 16. */ cl_int max_in_chunk = is_3d ? (is_dbl ? 384 : 800) : (is_dbl ? 448 : 896); if (is_matrix && is_3d && !is_dbl) max_in_chunk = 768; const size_t local_mem_size = max_in_chunk * (is_dbl ? sizeof(cl_double) : sizeof(cl_float)); error |= clSetKernelArg(k, arg++, sizeof(cl_int), &max_in_chunk); error |= clSetKernelArg(k, arg++, 2 * local_mem_size, 0); error |= clSetKernelArg(k, arg++, 2 * local_mem_size, 0); if (is_3d) error |= clSetKernelArg(k, arg++, local_mem_size, 0); } if (*status) return; if (error != CL_SUCCESS) { *status = OSKAR_ERR_INVALID_ARGUMENT; return; } /* Launch kernel on current command queue. */ local_size = is_gpu ? 256 : 128; global_size = ((num_out + local_size - 1) / local_size) * local_size; error = clEnqueueNDRangeKernel(oskar_cl_command_queue(), k, 1, NULL, &global_size, &local_size, 0, NULL, &event); if (error != CL_SUCCESS) *status = OSKAR_ERR_KERNEL_LAUNCH_FAILURE; #else *status = OSKAR_ERR_OPENCL_NOT_AVAILABLE; #endif } else if (location == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA if (is_data) { if (is_matrix) { if (is_3d) { if (is_dbl) oskar_dftw_m2m_3d_cuda_d(num_in, wavenumber, oskar_mem_double_const(x_in, status), oskar_mem_double_const(y_in, status), oskar_mem_double_const(z_in, status), oskar_mem_double2_const(weights_in, status), num_out, oskar_mem_double_const(x_out, status), oskar_mem_double_const(y_out, status), oskar_mem_double_const(z_out, status), oskar_mem_double4c_const(data, status), oskar_mem_double4c(output, status)); else oskar_dftw_m2m_3d_cuda_f(num_in, wavenumber, oskar_mem_float_const(x_in, status), oskar_mem_float_const(y_in, status), oskar_mem_float_const(z_in, status), oskar_mem_float2_const(weights_in, status), num_out, oskar_mem_float_const(x_out, status), oskar_mem_float_const(y_out, status), oskar_mem_float_const(z_out, status), oskar_mem_float4c_const(data, status), oskar_mem_float4c(output, status)); } else { if (is_dbl) oskar_dftw_m2m_2d_cuda_d(num_in, wavenumber, oskar_mem_double_const(x_in, status), oskar_mem_double_const(y_in, status), oskar_mem_double2_const(weights_in, status), num_out, oskar_mem_double_const(x_out, status), oskar_mem_double_const(y_out, status), oskar_mem_double4c_const(data, status), oskar_mem_double4c(output, status)); else oskar_dftw_m2m_2d_cuda_f(num_in, wavenumber, oskar_mem_float_const(x_in, status), oskar_mem_float_const(y_in, status), oskar_mem_float2_const(weights_in, status), num_out, oskar_mem_float_const(x_out, status), oskar_mem_float_const(y_out, status), oskar_mem_float4c_const(data, status), oskar_mem_float4c(output, status)); } } else { if (is_3d) { if (is_dbl) oskar_dftw_c2c_3d_cuda_d(num_in, wavenumber, oskar_mem_double_const(x_in, status), oskar_mem_double_const(y_in, status), oskar_mem_double_const(z_in, status), oskar_mem_double2_const(weights_in, status), num_out, oskar_mem_double_const(x_out, status), oskar_mem_double_const(y_out, status), oskar_mem_double_const(z_out, status), oskar_mem_double2_const(data, status), oskar_mem_double2(output, status)); else oskar_dftw_c2c_3d_cuda_f(num_in, wavenumber, oskar_mem_float_const(x_in, status), oskar_mem_float_const(y_in, status), oskar_mem_float_const(z_in, status), oskar_mem_float2_const(weights_in, status), num_out, oskar_mem_float_const(x_out, status), oskar_mem_float_const(y_out, status), oskar_mem_float_const(z_out, status), oskar_mem_float2_const(data, status), oskar_mem_float2(output, status)); } else { if (is_dbl) oskar_dftw_c2c_2d_cuda_d(num_in, wavenumber, oskar_mem_double_const(x_in, status), oskar_mem_double_const(y_in, status), oskar_mem_double2_const(weights_in, status), num_out, oskar_mem_double_const(x_out, status), oskar_mem_double_const(y_out, status), oskar_mem_double2_const(data, status), oskar_mem_double2(output, status)); else oskar_dftw_c2c_2d_cuda_f(num_in, wavenumber, oskar_mem_float_const(x_in, status), oskar_mem_float_const(y_in, status), oskar_mem_float2_const(weights_in, status), num_out, oskar_mem_float_const(x_out, status), oskar_mem_float_const(y_out, status), oskar_mem_float2_const(data, status), oskar_mem_float2(output, status)); } } } else { if (is_3d) { if (is_dbl) oskar_dftw_o2c_3d_cuda_d(num_in, wavenumber, oskar_mem_double_const(x_in, status), oskar_mem_double_const(y_in, status), oskar_mem_double_const(z_in, status), oskar_mem_double2_const(weights_in, status), num_out, oskar_mem_double_const(x_out, status), oskar_mem_double_const(y_out, status), oskar_mem_double_const(z_out, status), oskar_mem_double2(output, status)); else oskar_dftw_o2c_3d_cuda_f(num_in, wavenumber, oskar_mem_float_const(x_in, status), oskar_mem_float_const(y_in, status), oskar_mem_float_const(z_in, status), oskar_mem_float2_const(weights_in, status), num_out, oskar_mem_float_const(x_out, status), oskar_mem_float_const(y_out, status), oskar_mem_float_const(z_out, status), oskar_mem_float2(output, status)); } else { if (is_dbl) oskar_dftw_o2c_2d_cuda_d(num_in, wavenumber, oskar_mem_double_const(x_in, status), oskar_mem_double_const(y_in, status), oskar_mem_double2_const(weights_in, status), num_out, oskar_mem_double_const(x_out, status), oskar_mem_double_const(y_out, status), oskar_mem_double2(output, status)); else oskar_dftw_o2c_2d_cuda_f(num_in, wavenumber, oskar_mem_float_const(x_in, status), oskar_mem_float_const(y_in, status), oskar_mem_float2_const(weights_in, status), num_out, oskar_mem_float_const(x_out, status), oskar_mem_float_const(y_out, status), oskar_mem_float2(output, status)); } } oskar_device_check_error(status); #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } else if (location == OSKAR_CPU) { if (is_data) { if (is_matrix) { if (is_3d) { if (is_dbl) oskar_dftw_m2m_3d_omp_d(num_in, wavenumber, oskar_mem_double_const(x_in, status), oskar_mem_double_const(y_in, status), oskar_mem_double_const(z_in, status), oskar_mem_double2_const(weights_in, status), num_out, oskar_mem_double_const(x_out, status), oskar_mem_double_const(y_out, status), oskar_mem_double_const(z_out, status), oskar_mem_double4c_const(data, status), oskar_mem_double4c(output, status)); else oskar_dftw_m2m_3d_omp_f(num_in, wavenumber, oskar_mem_float_const(x_in, status), oskar_mem_float_const(y_in, status), oskar_mem_float_const(z_in, status), oskar_mem_float2_const(weights_in, status), num_out, oskar_mem_float_const(x_out, status), oskar_mem_float_const(y_out, status), oskar_mem_float_const(z_out, status), oskar_mem_float4c_const(data, status), oskar_mem_float4c(output, status)); } else { if (is_dbl) oskar_dftw_m2m_2d_omp_d(num_in, wavenumber, oskar_mem_double_const(x_in, status), oskar_mem_double_const(y_in, status), oskar_mem_double2_const(weights_in, status), num_out, oskar_mem_double_const(x_out, status), oskar_mem_double_const(y_out, status), oskar_mem_double4c_const(data, status), oskar_mem_double4c(output, status)); else oskar_dftw_m2m_2d_omp_f(num_in, wavenumber, oskar_mem_float_const(x_in, status), oskar_mem_float_const(y_in, status), oskar_mem_float2_const(weights_in, status), num_out, oskar_mem_float_const(x_out, status), oskar_mem_float_const(y_out, status), oskar_mem_float4c_const(data, status), oskar_mem_float4c(output, status)); } } else { if (is_3d) { if (is_dbl) oskar_dftw_c2c_3d_omp_d(num_in, wavenumber, oskar_mem_double_const(x_in, status), oskar_mem_double_const(y_in, status), oskar_mem_double_const(z_in, status), oskar_mem_double2_const(weights_in, status), num_out, oskar_mem_double_const(x_out, status), oskar_mem_double_const(y_out, status), oskar_mem_double_const(z_out, status), oskar_mem_double2_const(data, status), oskar_mem_double2(output, status)); else oskar_dftw_c2c_3d_omp_f(num_in, wavenumber, oskar_mem_float_const(x_in, status), oskar_mem_float_const(y_in, status), oskar_mem_float_const(z_in, status), oskar_mem_float2_const(weights_in, status), num_out, oskar_mem_float_const(x_out, status), oskar_mem_float_const(y_out, status), oskar_mem_float_const(z_out, status), oskar_mem_float2_const(data, status), oskar_mem_float2(output, status)); } else { if (is_dbl) oskar_dftw_c2c_2d_omp_d(num_in, wavenumber, oskar_mem_double_const(x_in, status), oskar_mem_double_const(y_in, status), oskar_mem_double2_const(weights_in, status), num_out, oskar_mem_double_const(x_out, status), oskar_mem_double_const(y_out, status), oskar_mem_double2_const(data, status), oskar_mem_double2(output, status)); else oskar_dftw_c2c_2d_omp_f(num_in, wavenumber, oskar_mem_float_const(x_in, status), oskar_mem_float_const(y_in, status), oskar_mem_float2_const(weights_in, status), num_out, oskar_mem_float_const(x_out, status), oskar_mem_float_const(y_out, status), oskar_mem_float2_const(data, status), oskar_mem_float2(output, status)); } } } else { if (is_3d) { if (is_dbl) oskar_dftw_o2c_3d_omp_d(num_in, wavenumber, oskar_mem_double_const(x_in, status), oskar_mem_double_const(y_in, status), oskar_mem_double_const(z_in, status), oskar_mem_double2_const(weights_in, status), num_out, oskar_mem_double_const(x_out, status), oskar_mem_double_const(y_out, status), oskar_mem_double_const(z_out, status), oskar_mem_double2(output, status)); else oskar_dftw_o2c_3d_omp_f(num_in, wavenumber, oskar_mem_float_const(x_in, status), oskar_mem_float_const(y_in, status), oskar_mem_float_const(z_in, status), oskar_mem_float2_const(weights_in, status), num_out, oskar_mem_float_const(x_out, status), oskar_mem_float_const(y_out, status), oskar_mem_float_const(z_out, status), oskar_mem_float2(output, status)); } else { if (is_dbl) oskar_dftw_o2c_2d_omp_d(num_in, wavenumber, oskar_mem_double_const(x_in, status), oskar_mem_double_const(y_in, status), oskar_mem_double2_const(weights_in, status), num_out, oskar_mem_double_const(x_out, status), oskar_mem_double_const(y_out, status), oskar_mem_double2(output, status)); else oskar_dftw_o2c_2d_omp_f(num_in, wavenumber, oskar_mem_float_const(x_in, status), oskar_mem_float_const(y_in, status), oskar_mem_float2_const(weights_in, status), num_out, oskar_mem_float_const(x_out, status), oskar_mem_float_const(y_out, status), oskar_mem_float2(output, status)); } } } else { *status = OSKAR_ERR_BAD_LOCATION; } }
davepallot/OSKAR
oskar/beam_pattern/src/oskar_beam_pattern_check_init.c
<gh_stars>1-10 /* * Copyright (c) 2012-2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "beam_pattern/oskar_beam_pattern.h" #include "beam_pattern/private_beam_pattern.h" #include "beam_pattern/private_beam_pattern_generate_coordinates.h" #include "convert/oskar_convert_fov_to_cellsize.h" #include "math/oskar_cmath.h" #include "math/private_cond2_2x2.h" #include "utility/oskar_device_utils.h" #include "utility/oskar_file_exists.h" #include "oskar_version.h" #include <stdlib.h> #include <string.h> #if __STDC_VERSION__ >= 199901L #define SNPRINTF(BUF, SIZE, FMT, ...) snprintf(BUF, SIZE, FMT, __VA_ARGS__); #else #define SNPRINTF(BUF, SIZE, FMT, ...) sprintf(BUF, FMT, __VA_ARGS__); #endif #ifdef __cplusplus extern "C" { #endif static void set_up_host_data(oskar_BeamPattern* h, int *status); static void create_averaged_products(oskar_BeamPattern* h, int ta, int ca, int* status); static void set_up_device_data(oskar_BeamPattern* h, int* status); static void write_axis(fitsfile* fptr, int axis_id, const char* ctype, const char* ctype_comment, double crval, double cdelt, double crpix, int* status); static fitsfile* create_fits_file(const char* filename, int precision, int width, int height, int num_times, int num_channels, double centre_deg[2], double fov_deg[2], double start_time_mjd, double delta_time_sec, double start_freq_hz, double delta_freq_hz, int horizon_mode, const char* settings_log, size_t settings_log_length, int* status); static int data_product_index(oskar_BeamPattern* h, int data_product_type, int stokes_in, int stokes_out, int i_station, int time_average, int channel_average); static char* construct_filename(oskar_BeamPattern* h, int data_product_type, int stokes_in, int stokes_out, int i_station, int time_average, int channel_average, const char* ext); static void new_fits_file(oskar_BeamPattern* h, int data_product_type, int stokes_in, int stokes_out, int i_station, int channel_average, int time_average, int* status); static void new_text_file(oskar_BeamPattern* h, int data_product_type, int stokes_in, int stokes_out, int i_station, int channel_average, int time_average, int* status); static const char* data_type_to_string(int type); static const char* stokes_type_to_string(int type); void oskar_beam_pattern_check_init(oskar_BeamPattern* h, int* status) { if (*status) return; /* Check that the telescope model has been set. */ if (!h->tel) { oskar_log_error(h->log, "Telescope model not set."); *status = OSKAR_ERR_SETTINGS_TELESCOPE; return; } /* Check that each compute device has been set up. */ set_up_host_data(h, status); set_up_device_data(h, status); } static void set_up_host_data(oskar_BeamPattern* h, int *status) { int i, k; size_t j; if (*status) return; /* Set up pixel positions. */ oskar_beam_pattern_generate_coordinates(h, OSKAR_SPHERICAL_TYPE_EQUATORIAL, status); /* Work out how many pixel chunks have to be processed. */ h->num_chunks = (h->num_pixels + h->max_chunk_size - 1) / h->max_chunk_size; /* Create scratch arrays for output pixel data. */ if (!h->pix) { h->pix = oskar_mem_create(h->prec, OSKAR_CPU, h->max_chunk_size, status); h->ctemp = oskar_mem_create(h->prec | OSKAR_COMPLEX, OSKAR_CPU, h->max_chunk_size, status); } /* Get the contents of the log at this point so we can write a * reasonable file header. Replace newlines with zeros. */ h->settings_log_length = 0; free(h->settings_log); h->settings_log = oskar_log_file_data(h->log, &h->settings_log_length); for (j = 0; j < h->settings_log_length; ++j) { if (h->settings_log[j] == '\n') h->settings_log[j] = 0; if (h->settings_log[j] == '\r') h->settings_log[j] = ' '; } /* Return if data products already exist. */ if (h->data_products) return; /* Create a file for each requested data product. */ /* Voltage amplitude and phase can only be generated if there is * no averaging. */ if (h->separate_time_and_channel) { /* Create station-level data products. */ for (i = 0; i < h->num_active_stations; ++i) { /* Text file. */ if (h->voltage_raw_txt) new_text_file(h, RAW_COMPLEX, -1, -1, i, 0, 0, status); if (h->voltage_amp_txt) { if (h->pol_mode == OSKAR_POL_MODE_SCALAR) new_text_file(h, AMP, -1, -1, i, 0, 0, status); else for (k = XX; k <= YY; ++k) new_text_file(h, AMP, -1, k, i, 0, 0, status); } if (h->voltage_phase_txt) { if (h->pol_mode == OSKAR_POL_MODE_SCALAR) new_text_file(h, PHASE, -1, -1, i, 0, 0, status); else for (k = XX; k <= YY; ++k) new_text_file(h, PHASE, -1, k, i, 0, 0, status); } if (h->ixr_txt && h->pol_mode == OSKAR_POL_MODE_FULL) new_text_file(h, IXR, -1, -1, i, 0, 0, status); /* Can only create images if coordinates are on a grid. */ if (h->coord_grid_type != 'B') continue; /* FITS file. */ if (h->voltage_amp_fits) { if (h->pol_mode == OSKAR_POL_MODE_SCALAR) new_fits_file(h, AMP, -1, -1, i, 0, 0, status); else for (k = XX; k <= YY; ++k) new_fits_file(h, AMP, -1, k, i, 0, 0, status); } if (h->voltage_phase_fits) { if (h->pol_mode == OSKAR_POL_MODE_SCALAR) new_fits_file(h, PHASE, -1, -1, i, 0, 0, status); else for (k = XX; k <= YY; ++k) new_fits_file(h, PHASE, -1, k, i, 0, 0, status); } if (h->ixr_fits && h->pol_mode == OSKAR_POL_MODE_FULL) new_fits_file(h, IXR, -1, -1, i, 0, 0, status); } } /* Create data products that can be averaged. */ if (h->separate_time_and_channel) create_averaged_products(h, 0, 0, status); if (h->average_time_and_channel) create_averaged_products(h, 1, 1, status); if (h->average_single_axis == 'C') create_averaged_products(h, 0, 1, status); else if (h->average_single_axis == 'T') create_averaged_products(h, 1, 0, status); /* Check that at least one output file will be generated. */ if (h->num_data_products == 0 && !*status) { *status = OSKAR_ERR_FILE_IO; oskar_log_error(h->log, "No output file(s) selected."); } } static void create_averaged_products(oskar_BeamPattern* h, int ta, int ca, int* status) { int s, i, o; if (*status) return; /* Create station-level data products that can be averaged. */ for (s = 0; s < h->num_active_stations; ++s) { /* Text file. */ for (i = I; i <= V; ++i) for (o = I; (o <= V) && h->stokes[i] && h->auto_power_txt; ++o) new_text_file(h, AUTO_POWER, i, o, s, ta, ca, status); /* Can only create images if coordinates are on a grid. */ if (h->coord_grid_type != 'B') continue; /* FITS file. */ for (i = I; i <= V; ++i) for (o = I; (o <= V) && h->stokes[i] && h->auto_power_fits; ++o) new_fits_file(h, AUTO_POWER, i, o, s, ta, ca, status); } /* Text file. */ for (i = I; i <= V; ++i) { if (h->cross_power_raw_txt && h->stokes[i]) new_text_file(h, CROSS_POWER_RAW_COMPLEX, i, -1, -1, ta, ca, status); for (o = I; (o <= V) && h->stokes[i]; ++o) { if (h->cross_power_amp_txt) new_text_file(h, CROSS_POWER_AMP, i, o, -1, ta, ca, status); if (h->cross_power_phase_txt) new_text_file(h, CROSS_POWER_PHASE, i, o, -1, ta, ca, status); } } /* Can only create images if coordinates are on a grid. */ if (h->coord_grid_type != 'B') return; /* FITS file. */ for (i = I; i <= V; ++i) { for (o = I; (o <= V) && h->stokes[i]; ++o) { if (h->cross_power_amp_fits) new_fits_file(h, CROSS_POWER_AMP, i, o, -1, ta, ca, status); if (h->cross_power_phase_fits) new_fits_file(h, CROSS_POWER_PHASE, i, o, -1, ta, ca, status); } } } static void write_axis(fitsfile* fptr, int axis_id, const char* ctype, const char* ctype_comment, double crval, double cdelt, double crpix, int* status) { char key[FLEN_KEYWORD], value[FLEN_VALUE], comment[FLEN_COMMENT]; int decimals = 10; strncpy(comment, ctype_comment, FLEN_COMMENT-1); strncpy(value, ctype, FLEN_VALUE-1); fits_make_keyn("CTYPE", axis_id, key, status); fits_write_key_str(fptr, key, value, comment, status); fits_make_keyn("CRVAL", axis_id, key, status); fits_write_key_dbl(fptr, key, crval, decimals, NULL, status); fits_make_keyn("CDELT", axis_id, key, status); fits_write_key_dbl(fptr, key, cdelt, decimals, NULL, status); fits_make_keyn("CRPIX", axis_id, key, status); fits_write_key_dbl(fptr, key, crpix, decimals, NULL, status); fits_make_keyn("CROTA", axis_id, key, status); fits_write_key_dbl(fptr, key, 0.0, decimals, NULL, status); } static fitsfile* create_fits_file(const char* filename, int precision, int width, int height, int num_times, int num_channels, double centre_deg[2], double fov_deg[2], double start_time_mjd, double delta_time_sec, double start_freq_hz, double delta_freq_hz, int horizon_mode, const char* settings_log, size_t settings_log_length, int* status) { int imagetype; long naxes[4], naxes_dummy[4] = {1l, 1l, 1l, 1l}; double delta; const double deg2rad = M_PI / 180.0; const double rad2deg = 180.0 / M_PI; fitsfile* f = 0; const char* line; size_t length; if (*status) return 0; /* Create a new FITS file and write the image headers. */ if (oskar_file_exists(filename)) remove(filename); imagetype = (precision == OSKAR_DOUBLE ? DOUBLE_IMG : FLOAT_IMG); naxes[0] = width; naxes[1] = height; naxes[2] = num_channels; naxes[3] = num_times; fits_create_file(&f, filename, status); fits_create_img(f, imagetype, 4, naxes_dummy, status); fits_write_date(f, status); fits_write_key_str(f, "TELESCOP", "OSKAR " OSKAR_VERSION_STR, 0, status); /* Write axis headers. */ if (horizon_mode) { delta = oskar_convert_fov_to_cellsize(M_PI, width); write_axis(f, 1, "-----SIN", "Azimuthal angle", 0.0, -delta * rad2deg, (width + 1) / 2.0, status); delta = oskar_convert_fov_to_cellsize(M_PI, height); write_axis(f, 2, "-----SIN", "Elevation", 90.0, delta * rad2deg, (height + 1) / 2.0, status); } else { delta = oskar_convert_fov_to_cellsize(fov_deg[0] * deg2rad, width); write_axis(f, 1, "RA---SIN", "Right Ascension", centre_deg[0], -delta * rad2deg, (width + 1) / 2.0, status); delta = oskar_convert_fov_to_cellsize(fov_deg[1] * deg2rad, height); write_axis(f, 2, "DEC--SIN", "Declination", centre_deg[1], delta * rad2deg, (height + 1) / 2.0, status); } write_axis(f, 3, "FREQ", "Frequency", start_freq_hz, delta_freq_hz, 1.0, status); write_axis(f, 4, "UTC", "Time", start_time_mjd * 86400.0, delta_time_sec, 1.0, status); /* Write other headers. */ fits_write_key_str(f, "TIMESYS", "UTC", NULL, status); fits_write_key_str(f, "TIMEUNIT", "s", "Time axis units", status); fits_write_key_dbl(f, "MJD-OBS", start_time_mjd, 10, "Start time", status); if (!horizon_mode) { fits_write_key_dbl(f, "OBSRA", centre_deg[0], 10, "RA", status); fits_write_key_dbl(f, "OBSDEC", centre_deg[1], 10, "DEC", status); } /* Write the settings log up to this point as HISTORY comments. */ line = settings_log; length = settings_log_length; for (; settings_log_length > 0;) { const char* eol; fits_write_history(f, line, status); eol = (const char*) memchr(line, '\0', length); if (!eol) break; eol += 1; length -= (eol - line); line = eol; } /* Update header keywords with the correct axis lengths. * Needs to be done here because CFITSIO doesn't let us write only the * file header with the correct axis lengths to start with. This trick * allows us to create a small dummy image block to write only the headers, * and not waste effort moving a huge block of zeros within the file. */ fits_update_key_lng(f, "NAXIS1", naxes[0], 0, status); fits_update_key_lng(f, "NAXIS2", naxes[1], 0, status); fits_update_key_lng(f, "NAXIS3", naxes[2], 0, status); fits_update_key_lng(f, "NAXIS4", naxes[3], 0, status); return f; } static int data_product_index(oskar_BeamPattern* h, int data_product_type, int stokes_in, int stokes_out, int i_station, int time_average, int channel_average) { int i; for (i = 0; i < h->num_data_products; ++i) if (h->data_products[i].type == data_product_type && h->data_products[i].stokes_in == stokes_in && h->data_products[i].stokes_out == stokes_out && h->data_products[i].i_station == i_station && h->data_products[i].time_average == time_average && h->data_products[i].channel_average == channel_average) break; if (i == h->num_data_products) { i = h->num_data_products++; h->data_products = realloc(h->data_products, h->num_data_products * sizeof(DataProduct)); memset(&(h->data_products[i]), 0, sizeof(DataProduct)); h->data_products[i].type = data_product_type; h->data_products[i].stokes_in = stokes_in; h->data_products[i].stokes_out = stokes_out; h->data_products[i].i_station = i_station; h->data_products[i].time_average = time_average; h->data_products[i].channel_average = channel_average; } return i; } static char* construct_filename(oskar_BeamPattern* h, int data_product_type, int stokes_in, int stokes_out, int i_station, int time_average, int channel_average, const char* ext) { int buflen, start = 0; char* name = 0; /* Construct the filename. */ buflen = (int) strlen(h->root_path) + 100; name = calloc(buflen, 1); start += SNPRINTF(name + start, buflen - start, "%s", h->root_path); if (i_station >= 0) start += SNPRINTF(name + start, buflen - start, "_S%04d", h->station_ids[i_station]); start += SNPRINTF(name + start, buflen - start, "_%s", time_average ? "TIME_AVG" : "TIME_SEP"); start += SNPRINTF(name + start, buflen - start, "_%s", channel_average ? "CHAN_AVG" : "CHAN_SEP"); start += SNPRINTF(name + start, buflen - start, "_%s", data_type_to_string(data_product_type)); if (stokes_in >= 0) start += SNPRINTF(name + start, buflen - start, "_%s", stokes_type_to_string(stokes_in)); if (stokes_out >= 0) start += SNPRINTF(name + start, buflen - start, "_%s", stokes_type_to_string(stokes_out)); start += SNPRINTF(name + start, buflen - start, ".%s", ext); return name; } static void new_fits_file(oskar_BeamPattern* h, int data_product_type, int stokes_in, int stokes_out, int i_station, int time_average, int channel_average, int* status) { int i, horizon_mode; char* name; fitsfile* f; if (*status) return; /* Check polarisation type is possible. */ if ((stokes_in > I || stokes_out > I) && h->pol_mode != OSKAR_POL_MODE_FULL) return; /* Construct the filename. */ name = construct_filename(h, data_product_type, stokes_in, stokes_out, i_station, time_average, channel_average, "fits"); /* Open the file. */ horizon_mode = h->coord_frame_type == 'H'; f = create_fits_file(name, h->prec, h->width, h->height, (time_average ? 1 : h->num_time_steps), (channel_average ? 1 : h->num_channels), h->phase_centre_deg, h->fov_deg, h->time_start_mjd_utc, h->time_inc_sec, h->freq_start_hz, h->freq_inc_hz, horizon_mode, h->settings_log, h->settings_log_length, status); if (!f || *status) { *status = OSKAR_ERR_FILE_IO; free(name); return; } i = data_product_index(h, data_product_type, stokes_in, stokes_out, i_station, time_average, channel_average); h->data_products[i].fits_file = f; free(name); } static void new_text_file(oskar_BeamPattern* h, int data_product_type, int stokes_in, int stokes_out, int i_station, int time_average, int channel_average, int* status) { int i; char* name; FILE* f; if (*status) return; /* Check polarisation type is possible. */ if ((stokes_in > I || stokes_out > I) && h->pol_mode != OSKAR_POL_MODE_FULL) return; /* Construct the filename. */ name = construct_filename(h, data_product_type, stokes_in, stokes_out, i_station, time_average, channel_average, "txt"); /* Open the file. */ f = fopen(name, "w"); if (!f) { *status = OSKAR_ERR_FILE_IO; free(name); return; } if (i_station >= 0) fprintf(f, "# Beam pixel list for station %d\n", h->station_ids[i_station]); else fprintf(f, "# Beam pixel list for telescope (interferometer)\n"); fprintf(f, "# Filename is '%s'\n", name); fprintf(f, "# Dimension order (slowest to fastest) is:\n"); if (h->average_single_axis != 'T') fprintf(f, "# [pixel chunk], [time], [channel], [pixel index]\n"); else fprintf(f, "# [pixel chunk], [channel], [time], [pixel index]\n"); fprintf(f, "# Number of pixel chunks: %d\n", h->num_chunks); fprintf(f, "# Number of times (output): %d\n", time_average ? 1 : h->num_time_steps); fprintf(f, "# Number of channels (output): %d\n", channel_average ? 1 : h->num_channels); fprintf(f, "# Maximum pixel chunk size: %d\n", h->max_chunk_size); fprintf(f, "# Total number of pixels: %d\n", h->num_pixels); i = data_product_index(h, data_product_type, stokes_in, stokes_out, i_station, time_average, channel_average); h->data_products[i].text_file = f; free(name); } static const char* data_type_to_string(int type) { switch (type) { case RAW_COMPLEX: return "RAW_COMPLEX"; case AMP: return "AMP"; case PHASE: return "PHASE"; case AUTO_POWER: return "AUTO_POWER"; case CROSS_POWER_RAW_COMPLEX: return "CROSS_POWER_RAW_COMPLEX"; case CROSS_POWER_AMP: return "CROSS_POWER_AMP"; case CROSS_POWER_PHASE: return "CROSS_POWER_PHASE"; case IXR: return "IXR"; default: return ""; } } static const char* stokes_type_to_string(int type) { switch (type) { case I: return "I"; case Q: return "Q"; case U: return "U"; case V: return "V"; case XX: return "XX"; case XY: return "XY"; case YX: return "YX"; case YY: return "YY"; default: return ""; } } static void set_up_device_data(oskar_BeamPattern* h, int* status) { int i, beam_type, max_src, max_size, auto_power, cross_power, raw_data; if (*status) return; /* Get local variables. */ max_src = h->max_chunk_size; max_size = h->num_active_stations * max_src; beam_type = h->prec | OSKAR_COMPLEX; if (h->pol_mode == OSKAR_POL_MODE_FULL) beam_type |= OSKAR_MATRIX; raw_data = h->ixr_txt || h->ixr_fits || h->voltage_raw_txt || h->voltage_amp_txt || h->voltage_phase_txt || h->voltage_amp_fits || h->voltage_phase_fits; auto_power = h->auto_power_fits || h->auto_power_txt; cross_power = h->cross_power_raw_txt || h->cross_power_amp_fits || h->cross_power_phase_fits || h->cross_power_amp_txt || h->cross_power_phase_txt; /* Expand the number of devices to the number of selected GPUs, * if required. */ if (h->num_devices < h->num_gpus) oskar_beam_pattern_set_num_devices(h, h->num_gpus); for (i = 0; i < h->num_devices; ++i) { int dev_loc, i_stokes; DeviceData* d = &h->d[i]; if (*status) break; /* Select the device. */ if (i < h->num_gpus) { oskar_device_set(h->gpu_ids[i], status); dev_loc = OSKAR_GPU; } else { dev_loc = OSKAR_CPU; } /* Device memory. */ d->previous_chunk_index = -1; if (!d->tel) { d->jones_data = oskar_mem_create(beam_type, dev_loc, max_size, status); d->x = oskar_mem_create(h->prec, dev_loc, 1 + max_src, status); d->y = oskar_mem_create(h->prec, dev_loc, 1 + max_src, status); d->z = oskar_mem_create(h->prec, dev_loc, 1 + max_src, status); d->tel = oskar_telescope_create_copy(h->tel, dev_loc, status); d->work = oskar_station_work_create(h->prec, dev_loc, status); } /* Host memory. */ if (!d->jones_data_cpu[0] && raw_data) { d->jones_data_cpu[0] = oskar_mem_create(beam_type, OSKAR_CPU, max_size, status); d->jones_data_cpu[1] = oskar_mem_create(beam_type, OSKAR_CPU, max_size, status); } /* Auto-correlation beam output arrays. */ for (i_stokes = 0; i_stokes < 4; ++i_stokes) { if (!h->stokes[i_stokes]) continue; if (!d->auto_power[i_stokes] && auto_power) { /* Device memory. */ d->auto_power[i_stokes] = oskar_mem_create(beam_type, dev_loc, max_size, status); /* Host memory. */ d->auto_power_cpu[i_stokes][0] = oskar_mem_create( beam_type, OSKAR_CPU, max_size, status); d->auto_power_cpu[i_stokes][1] = oskar_mem_create( beam_type, OSKAR_CPU, max_size, status); if (h->average_single_axis == 'T') d->auto_power_time_avg[i_stokes] = oskar_mem_create( beam_type, OSKAR_CPU, max_size, status); if (h->average_single_axis == 'C') d->auto_power_channel_avg[i_stokes] = oskar_mem_create( beam_type, OSKAR_CPU, max_size, status); if (h->average_time_and_channel) d->auto_power_channel_and_time_avg[i_stokes] = oskar_mem_create(beam_type, OSKAR_CPU, max_size, status); } /* Cross-correlation beam output arrays. */ if (!d->cross_power[i_stokes] && cross_power) { if (h->num_active_stations < 2) { oskar_log_error(h->log, "Cannot create cross-power beam " "using less than two active stations."); *status = OSKAR_ERR_INVALID_ARGUMENT; break; } /* Device memory. */ d->cross_power[i_stokes] = oskar_mem_create( beam_type, dev_loc, max_src, status); /* Host memory. */ d->cross_power_cpu[i_stokes][0] = oskar_mem_create( beam_type, OSKAR_CPU, max_src, status); d->cross_power_cpu[i_stokes][1] = oskar_mem_create( beam_type, OSKAR_CPU, max_src, status); if (h->average_single_axis == 'T') d->cross_power_time_avg[i_stokes] = oskar_mem_create( beam_type, OSKAR_CPU, max_src, status); if (h->average_single_axis == 'C') d->cross_power_channel_avg[i_stokes] = oskar_mem_create( beam_type, OSKAR_CPU, max_src, status); if (h->average_time_and_channel) d->cross_power_channel_and_time_avg[i_stokes] = oskar_mem_create(beam_type, OSKAR_CPU, max_src, status); } if (d->auto_power[i_stokes]) oskar_mem_clear_contents(d->auto_power[i_stokes], status); if (d->cross_power[i_stokes]) oskar_mem_clear_contents(d->cross_power[i_stokes], status); } /* Timers. */ if (!d->tmr_compute) d->tmr_compute = oskar_timer_create(OSKAR_TIMER_NATIVE); } } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/utility/src/oskar_dir.c
/* * Copyright (c) 2016, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "utility/oskar_dir.h" #ifndef OSKAR_OS_WIN #include <dirent.h> #include <errno.h> #include <sys/stat.h> #else #define WIN32_LEAN_AND_MEAN #include <windows.h> #endif #include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef __cplusplus extern "C" { #endif int oskar_dir_exists(const char* dir_path) { #ifndef OSKAR_OS_WIN struct stat s; return (!stat(dir_path, &s) && S_ISDIR(s.st_mode)) ? 1 : 0; #else int attrib = GetFileAttributes(dir_path); return (attrib != INVALID_FILE_ATTRIBUTES && (attrib & FILE_ATTRIBUTE_DIRECTORY)); #endif } int oskar_dir_file_exists(const char* dir_path, const char* file_name) { FILE* f = 0; char* path = 0; path = oskar_dir_get_path(dir_path, file_name); f = fopen(path, "rb"); if (f) { fclose(f); free(path); return 1; } free(path); return 0; } char* oskar_dir_get_path(const char* dir_path, const char* item_name) { char* buffer = 0; int buf_len = 0, dir_path_len; dir_path_len = (int) strlen(dir_path); buf_len = 2 + dir_path_len + (int) strlen(item_name); buffer = (char*) calloc(buf_len, sizeof(char)); if (dir_path[dir_path_len - 1] == oskar_dir_separator()) sprintf(buffer, "%s%s", dir_path, item_name); else sprintf(buffer, "%s%c%s", dir_path, oskar_dir_separator(), item_name); return buffer; } static int name_cmp(const void* a, const void* b) { return strcmp(*(char* const*)a, *(char* const*)b); } /* From http://c-faq.com/lib/regex.html */ static int match(const char *pattern, const char *str) { switch (*pattern) { case '\0': return !*str; case '*': return match(pattern+1, str) || (*str && match(pattern, str+1)); case '?': return *str && match(pattern+1, str+1); default: return *pattern == *str && match(pattern+1, str+1); } } static void item(const char* dir_path, const char* name, const char* wildcard, int match_files, int match_dirs, int* i, int num_items, char** items) { if (!strcmp(name, ".") || !strcmp(name, "..")) return; if (wildcard && !match(wildcard, name)) return; if (match_files ^ match_dirs) { int is_dir = 0, rejected = 0; char* item_path; item_path = oskar_dir_get_path(dir_path, name); is_dir = oskar_dir_exists(item_path); if ((is_dir && !match_dirs) || (!is_dir && match_dirs)) rejected = 1; free(item_path); if (rejected) return; } if (items && *i < num_items) { items[*i] = (char*) realloc(items[*i], 1 + strlen(name)); strcpy(items[*i], name); } ++(*i); } static int get_items(const char* dir_path, const char* wildcard, int match_files, int match_dirs, int num_items, char** items) { int i = 0; #ifndef OSKAR_OS_WIN DIR *d; struct dirent *t; if (!(d = opendir(dir_path))) return 0; while ((t = readdir(d)) != 0) item(dir_path, t->d_name, wildcard, match_files, match_dirs, &i, num_items, items); (void) closedir(d); #else WIN32_FIND_DATA f; HANDLE h = 0; char* buffer = 0; buffer = (char*) malloc(3 + strlen(dir_path)); (void) sprintf(buffer, "%s\\*", dir_path); if ((h = FindFirstFile(buffer, &f)) == INVALID_HANDLE_VALUE) { free(buffer); return 0; } do item(dir_path, f.cFileName, wildcard, match_files, match_dirs, &i, num_items, items); while (FindNextFile(h, &f)); FindClose(h); free(buffer); #endif return i; } void oskar_dir_items(const char* dir_path, const char* wildcard, int match_files, int match_dirs, int* num_items, char*** items) { int i = 0, old_num_items = *num_items; /* Count the number of items. */ *num_items = get_items(dir_path, wildcard, match_files, match_dirs, 0, 0); /* Get the sorted list of names if required. */ if (items) { for (i = *num_items; i < old_num_items; ++i) free((*items)[i]); *items = (char**) realloc(*items, *num_items * sizeof(char**)); for (i = old_num_items; i < *num_items; ++i) (*items)[i] = 0; (void) get_items(dir_path, wildcard, match_files, match_dirs, *num_items, *items); qsort(*items, *num_items, sizeof(char*), name_cmp); } } int oskar_dir_mkdir(const char* dir_path) { #ifndef OSKAR_OS_WIN struct stat s; if (stat(dir_path, &s) != 0) { /* Item does not exist. Try to create the directory. */ if (mkdir(dir_path, 0755) != 0 && errno != EEXIST) return 0; } else if (!S_ISDIR(s.st_mode)) return 0; #else int attrib; if ((attrib = GetFileAttributes(dir_path)) == INVALID_FILE_ATTRIBUTES) { /* Item does not exist. Try to create the directory. */ if (!CreateDirectory(dir_path, 0) && GetLastError() != ERROR_ALREADY_EXISTS) return 0; } else if (!(attrib & FILE_ATTRIBUTE_DIRECTORY)) return 0; #endif return 1; } int oskar_dir_mkpath(const char* dir_path) { char *start, *sep, *dir_path_p; int error = 0; size_t path_len; /* Copy the input path string so it can be modified. */ path_len = 1 + strlen(dir_path); dir_path_p = malloc(path_len); if (!dir_path_p) return 0; memcpy(dir_path_p, dir_path, path_len); /* Loop over directories in path to ensure they all exist. */ start = dir_path_p; while (!error && (sep = strchr(start, oskar_dir_separator()))) { if (sep != start && *(sep - 1) != ':') { *sep = '\0'; /* Temporarily truncate to ensure this dir exists. */ error = !oskar_dir_mkdir(dir_path_p); *sep = oskar_dir_separator(); /* Restore full path. */ } start = sep + 1; } free(dir_path_p); return !error ? oskar_dir_mkdir(dir_path) : 0; } int oskar_dir_remove(const char* dir_path) { int error = 0, i, num_items = 0; char **items = 0, *path = 0; if (!oskar_dir_exists(dir_path) || !strcmp(dir_path, ".") || !strcmp(dir_path, "./") || !strcmp(dir_path, ".\\")) return 0; /* Get names of all items in the directory. */ oskar_dir_items(dir_path, NULL, 1, 1, &num_items, &items); for (i = 0; i < num_items; ++i) { /* Get full path of the item. */ path = oskar_dir_get_path(dir_path, items[i]); /* Remove files and directories recursively. */ if (!oskar_dir_exists(path)) error = remove(path); else error = !oskar_dir_remove(path); free(path); if (error) break; } for (i = 0; i < num_items; ++i) free(items[i]); free(items); if (error) return 0; /* Remove the empty directory. */ #ifdef OSKAR_OS_WIN return RemoveDirectory(dir_path); #else return remove(dir_path) ? 0 : 1; #endif } char oskar_dir_separator(void) { #ifdef OSKAR_OS_WIN return '\\'; #else return '/'; #endif } #ifdef __cplusplus } #endif
davepallot/OSKAR
gui/oskar_RunDialog.h
<filename>gui/oskar_RunDialog.h /* * Copyright (c) 2012-2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_RUN_DIALOG_H_ #define OSKAR_RUN_DIALOG_H_ #include <QtWidgets/QDialog> #include <QtCore/QProcess> class QAbstractButton; class QCheckBox; class QCloseEvent; class QDialogButtonBox; class QLabel; class QPushButton; class QTextEdit; namespace oskar { class RunDialog : public QDialog { Q_OBJECT public: RunDialog(const QString& app, QWidget *parent = 0); ~RunDialog(); void setAllowAutoClose(bool value); void start(const QStringList& args = QStringList()); protected: void closeEvent(QCloseEvent*); private slots: void buttonClicked(QAbstractButton* button); void readProcess(); void runAborted(); void runCompleted(); void runCrashed(); void runFailed(); void runFinished(int exitCode, QProcess::ExitStatus exitStatus); private: bool aborted_; bool failed_; bool finished_; QCheckBox* autoClose_; QTextEdit* display_; QLabel* labelText_; QLabel* labelCommand_; QDialogButtonBox* buttons_; QPushButton* closeButton_; QPushButton* cancelButton_; QString app_; QProcess* process_; }; } /* namespace oskar */ #endif /* OSKAR_RUN_DIALOG_H_ */
davepallot/OSKAR
oskar/convert/src/oskar_convert_enu_directions_to_cirs_relative_directions.c
/* * Copyright (c) 2014-2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "convert/oskar_convert_enu_directions_to_cirs_relative_directions.h" #include "convert/oskar_convert_enu_directions_to_cirs_relative_directions_cuda.h" #include "convert/private_convert_enu_directions_to_cirs_relative_directions_inline.h" #include "convert/private_evaluate_cirs_observed_parameters.h" #ifdef __cplusplus extern "C" { #endif /* Single precision. */ void oskar_convert_enu_directions_to_cirs_relative_directions_f( int num_points, const float* x, const float* y, const float* z, float ra0_rad, float dec0_rad, float lon_rad, float lat_rad, float era_rad, float pm_x_rad, float pm_y_rad, float diurnal_aberration, float* l, float* m, float* n) { int i; double sin_lat, cos_lat, sin_ha0, cos_ha0, sin_dec0, cos_dec0; double local_pm_x, local_pm_y; /* Calculate common transform parameters. */ oskar_evaluate_cirs_observed_parameters(lon_rad, lat_rad, era_rad, ra0_rad, dec0_rad, pm_x_rad, pm_y_rad, &sin_lat, &cos_lat, &sin_ha0, &cos_ha0, &sin_dec0, &cos_dec0, &local_pm_x, &local_pm_y); /* Loop over positions. */ for (i = 0; i < num_points; ++i) { oskar_convert_enu_directions_to_cirs_relative_directions_inline_f( x[i], y[i], z[i], (float)sin_lat, (float)cos_lat, (float)sin_ha0, (float)cos_ha0, (float)sin_dec0, (float)cos_dec0, (float)local_pm_x, (float)local_pm_y, diurnal_aberration, &l[i], &m[i], &n[i]); } } /* Double precision. */ void oskar_convert_enu_directions_to_cirs_relative_directions_d( int num_points, const double* x, const double* y, const double* z, double ra0_rad, double dec0_rad, double lon_rad, double lat_rad, double era_rad, double pm_x_rad, double pm_y_rad, double diurnal_aberration, double* l, double* m, double* n) { int i; double sin_lat, cos_lat, sin_ha0, cos_ha0, sin_dec0, cos_dec0; double local_pm_x, local_pm_y; /* Calculate common transform parameters. */ oskar_evaluate_cirs_observed_parameters(lon_rad, lat_rad, era_rad, ra0_rad, dec0_rad, pm_x_rad, pm_y_rad, &sin_lat, &cos_lat, &sin_ha0, &cos_ha0, &sin_dec0, &cos_dec0, &local_pm_x, &local_pm_y); /* Loop over positions. */ for (i = 0; i < num_points; ++i) { oskar_convert_enu_directions_to_cirs_relative_directions_inline_d( x[i], y[i], z[i], sin_lat, cos_lat, sin_ha0, cos_ha0, sin_dec0, cos_dec0, local_pm_x, local_pm_y, diurnal_aberration, &l[i], &m[i], &n[i]); } } /* Wrapper. */ void oskar_convert_enu_directions_to_cirs_relative_directions( int num_points, const oskar_Mem* x, const oskar_Mem* y, const oskar_Mem* z, double ra0_rad, double dec0_rad, double lon_rad, double lat_rad, double era_rad, double pm_x_rad, double pm_y_rad, double diurnal_aberration, oskar_Mem* l, oskar_Mem* m, oskar_Mem* n, int* status) { int type, location; /* Check if safe to proceed. */ if (*status) return; /* Get type and check consistency. */ type = oskar_mem_type(x); if (type != OSKAR_SINGLE && type != OSKAR_DOUBLE) { *status = OSKAR_ERR_BAD_DATA_TYPE; return; } if (type != oskar_mem_type(y) || type != oskar_mem_type(z) || type != oskar_mem_type(l) || type != oskar_mem_type(m) || type != oskar_mem_type(n)) { *status = OSKAR_ERR_TYPE_MISMATCH; return; } /* Get location and check consistency. */ location = oskar_mem_location(x); if (location != oskar_mem_location(y) || location != oskar_mem_location(z) || location != oskar_mem_location(l) || location != oskar_mem_location(m) || location != oskar_mem_location(n)) { *status = OSKAR_ERR_LOCATION_MISMATCH; return; } /* Check dimension consistency. */ if ((int)oskar_mem_length(x) < num_points || (int)oskar_mem_length(y) < num_points || (int)oskar_mem_length(z) < num_points || (int)oskar_mem_length(l) < num_points || (int)oskar_mem_length(m) < num_points || (int)oskar_mem_length(n) < num_points) { *status = OSKAR_ERR_DIMENSION_MISMATCH; return; } /* Switch on type and location. */ if (type == OSKAR_DOUBLE) { double *l_, *m_, *n_; const double *x_, *y_, *z_; l_ = oskar_mem_double(l, status); m_ = oskar_mem_double(m, status); n_ = oskar_mem_double(n, status); x_ = oskar_mem_double_const(x, status); y_ = oskar_mem_double_const(y, status); z_ = oskar_mem_double_const(z, status); if (location == OSKAR_CPU) { oskar_convert_enu_directions_to_cirs_relative_directions_d( num_points, x_, y_, z_, ra0_rad, dec0_rad, lon_rad, lat_rad, era_rad, pm_x_rad, pm_y_rad, diurnal_aberration, l_, m_, n_); } else if (location == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA oskar_convert_enu_directions_to_cirs_relative_directions_cuda_d( num_points, x_, y_, z_, ra0_rad, dec0_rad, lon_rad, lat_rad, era_rad, pm_x_rad, pm_y_rad, diurnal_aberration, l_, m_, n_); #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } } else { float *l_, *m_, *n_; const float *x_, *y_, *z_; l_ = oskar_mem_float(l, status); m_ = oskar_mem_float(m, status); n_ = oskar_mem_float(n, status); x_ = oskar_mem_float_const(x, status); y_ = oskar_mem_float_const(y, status); z_ = oskar_mem_float_const(z, status); if (location == OSKAR_CPU) { oskar_convert_enu_directions_to_cirs_relative_directions_f( num_points, x_, y_, z_, ra0_rad, dec0_rad, lon_rad, lat_rad, era_rad, pm_x_rad, pm_y_rad, diurnal_aberration, l_, m_, n_); } else if (location == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA oskar_convert_enu_directions_to_cirs_relative_directions_cuda_f( num_points, x_, y_, z_, ra0_rad, dec0_rad, lon_rad, lat_rad, era_rad, pm_x_rad, pm_y_rad, diurnal_aberration, l_, m_, n_); #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } } } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/imager/src/oskar_grid_simple.c
/* * Copyright (c) 2016-2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "imager/oskar_grid_simple.h" #include <math.h> #include <stdlib.h> #ifdef __cplusplus extern "C" { #endif #define D_SUPPORT 3 #define D_OVERSAMPLE 100 static void oskar_grid_simple_default_d( const double* restrict conv_func, const size_t num_points, const double* restrict uu, const double* restrict vv, const double* restrict vis, const double* restrict weight, const double cell_size_rad, const int grid_size, size_t* restrict num_skipped, double* restrict norm, double* restrict grid) { size_t i; const int grid_centre = grid_size / 2; const double grid_scale = grid_size * cell_size_rad; /* Loop over visibilities. */ *num_skipped = 0; for (i = 0; i < num_points; ++i) { double sum = 0.0; int j, k; /* Convert UV coordinates to grid coordinates. */ const double pos_u = -uu[i] * grid_scale; const double pos_v = vv[i] * grid_scale; const int grid_u = (int)round(pos_u) + grid_centre; const int grid_v = (int)round(pos_v) + grid_centre; /* Get visibility data. */ const double weight_i = weight[i]; const double v_re = weight_i * vis[2 * i]; const double v_im = weight_i * vis[2 * i + 1]; /* Scaled distance from nearest grid point. */ const int off_u = (int)round((round(pos_u) - pos_u) * D_OVERSAMPLE); const int off_v = (int)round((round(pos_v) - pos_v) * D_OVERSAMPLE); /* Catch points that would lie outside the grid. */ if (grid_u + D_SUPPORT >= grid_size || grid_u - D_SUPPORT < 0 || grid_v + D_SUPPORT >= grid_size || grid_v - D_SUPPORT < 0) { *num_skipped += 1; continue; } /* Convolve this point onto the grid. */ for (j = -D_SUPPORT; j <= D_SUPPORT; ++j) { size_t p1; const double c1 = conv_func[abs(off_v + j * D_OVERSAMPLE)]; p1 = grid_v + j; p1 *= grid_size; /* Tested to avoid int overflow. */ p1 += grid_u; for (k = -D_SUPPORT; k <= D_SUPPORT; ++k) { const size_t p = (p1 + k) << 1; const double c = conv_func[abs(off_u + k * D_OVERSAMPLE)] * c1; grid[p] += v_re * c; grid[p + 1] += v_im * c; sum += c; } } *norm += sum * weight_i; } } static void oskar_grid_simple_default_f( const float* restrict conv_func, const size_t num_points, const float* restrict uu, const float* restrict vv, const float* restrict vis, const float* restrict weight, const float cell_size_rad, const int grid_size, size_t* restrict num_skipped, double* restrict norm, float* restrict grid) { size_t i; const int grid_centre = grid_size / 2; const float grid_scale = grid_size * cell_size_rad; /* Loop over visibilities. */ *num_skipped = 0; for (i = 0; i < num_points; ++i) { double sum = 0.0; int j, k; /* Convert UV coordinates to grid coordinates. */ const float pos_u = -uu[i] * grid_scale; const float pos_v = vv[i] * grid_scale; const int grid_u = (int)roundf(pos_u) + grid_centre; const int grid_v = (int)roundf(pos_v) + grid_centre; /* Get visibility data. */ const float weight_i = weight[i]; const float v_re = weight_i * vis[2 * i]; const float v_im = weight_i * vis[2 * i + 1]; /* Scaled distance from nearest grid point. */ const int off_u = (int)roundf((roundf(pos_u) - pos_u) * D_OVERSAMPLE); const int off_v = (int)roundf((roundf(pos_v) - pos_v) * D_OVERSAMPLE); /* Catch points that would lie outside the grid. */ if (grid_u + D_SUPPORT >= grid_size || grid_u - D_SUPPORT < 0 || grid_v + D_SUPPORT >= grid_size || grid_v - D_SUPPORT < 0) { *num_skipped += 1; continue; } /* Convolve this point onto the grid. */ for (j = -D_SUPPORT; j <= D_SUPPORT; ++j) { size_t p1; const float c1 = conv_func[abs(off_v + j * D_OVERSAMPLE)]; p1 = grid_v + j; p1 *= grid_size; /* Tested to avoid int overflow. */ p1 += grid_u; for (k = -D_SUPPORT; k <= D_SUPPORT; ++k) { const size_t p = (p1 + k) << 1; const float c = conv_func[abs(off_u + k * D_OVERSAMPLE)] * c1; grid[p] += v_re * c; grid[p + 1] += v_im * c; sum += c; } } *norm += sum * weight_i; } } void oskar_grid_simple_d( const int support, const int oversample, const double* restrict conv_func, const size_t num_points, const double* restrict uu, const double* restrict vv, const double* restrict vis, const double* restrict weight, const double cell_size_rad, const int grid_size, size_t* restrict num_skipped, double* restrict norm, double* restrict grid) { size_t i; const int grid_centre = grid_size / 2; const double grid_scale = grid_size * cell_size_rad; /* Use slightly more efficient version for default parameters. */ if (support == D_SUPPORT && oversample == D_OVERSAMPLE) { oskar_grid_simple_default_d(conv_func, num_points, uu, vv, vis, weight, cell_size_rad, grid_size, num_skipped, norm, grid); return; } /* Loop over visibilities. */ *num_skipped = 0; for (i = 0; i < num_points; ++i) { double sum = 0.0; int j, k; /* Convert UV coordinates to grid coordinates. */ const double pos_u = -uu[i] * grid_scale; const double pos_v = vv[i] * grid_scale; const int grid_u = (int)round(pos_u) + grid_centre; const int grid_v = (int)round(pos_v) + grid_centre; /* Get visibility data. */ const double weight_i = weight[i]; const double v_re = weight_i * vis[2 * i]; const double v_im = weight_i * vis[2 * i + 1]; /* Scaled distance from nearest grid point. */ const int off_u = (int)round((round(pos_u) - pos_u) * oversample); const int off_v = (int)round((round(pos_v) - pos_v) * oversample); /* Catch points that would lie outside the grid. */ if (grid_u + support >= grid_size || grid_u - support < 0 || grid_v + support >= grid_size || grid_v - support < 0) { *num_skipped += 1; continue; } /* Convolve this point onto the grid. */ for (j = -support; j <= support; ++j) { size_t p1; const double c1 = conv_func[abs(off_v + j * oversample)]; p1 = grid_v + j; p1 *= grid_size; /* Tested to avoid int overflow. */ p1 += grid_u; for (k = -support; k <= support; ++k) { const size_t p = (p1 + k) << 1; const double c = conv_func[abs(off_u + k * oversample)] * c1; grid[p] += v_re * c; grid[p + 1] += v_im * c; sum += c; } } *norm += sum * weight_i; } } void oskar_grid_simple_f( const int support, const int oversample, const float* restrict conv_func, const size_t num_points, const float* restrict uu, const float* restrict vv, const float* restrict vis, const float* restrict weight, const float cell_size_rad, const int grid_size, size_t* restrict num_skipped, double* restrict norm, float* restrict grid) { size_t i; const int grid_centre = grid_size / 2; const float grid_scale = grid_size * cell_size_rad; /* Use slightly more efficient version for default parameters. */ if (support == D_SUPPORT && oversample == D_OVERSAMPLE) { oskar_grid_simple_default_f(conv_func, num_points, uu, vv, vis, weight, cell_size_rad, grid_size, num_skipped, norm, grid); return; } /* Loop over visibilities. */ *num_skipped = 0; for (i = 0; i < num_points; ++i) { double sum = 0.0; int j, k; /* Convert UV coordinates to grid coordinates. */ const float pos_u = -uu[i] * grid_scale; const float pos_v = vv[i] * grid_scale; const int grid_u = (int)roundf(pos_u) + grid_centre; const int grid_v = (int)roundf(pos_v) + grid_centre; /* Get visibility data. */ const float weight_i = weight[i]; const float v_re = weight_i * vis[2 * i]; const float v_im = weight_i * vis[2 * i + 1]; /* Scaled distance from nearest grid point. */ const int off_u = (int)roundf((roundf(pos_u) - pos_u) * oversample); const int off_v = (int)roundf((roundf(pos_v) - pos_v) * oversample); /* Catch points that would lie outside the grid. */ if (grid_u + support >= grid_size || grid_u - support < 0 || grid_v + support >= grid_size || grid_v - support < 0) { *num_skipped += 1; continue; } /* Convolve this point onto the grid. */ for (j = -support; j <= support; ++j) { size_t p1; const float c1 = conv_func[abs(off_v + j * oversample)]; p1 = grid_v + j; p1 *= grid_size; /* Tested to avoid int overflow. */ p1 += grid_u; for (k = -support; k <= support; ++k) { const size_t p = (p1 + k) << 1; const float c = conv_func[abs(off_u + k * oversample)] * c1; grid[p] += v_re * c; grid[p + 1] += v_im * c; sum += c; } } *norm += sum * weight_i; } } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/convert/oskar_convert_relative_directions_to_enu_directions_cuda.h
/* * Copyright (c) 2013-2014, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_CONVERT_RELATIVE_DIRECTIONS_TO_ENU_DIRECTIONS_CUDA_H_ #define OSKAR_CONVERT_RELATIVE_DIRECTIONS_TO_ENU_DIRECTIONS_CUDA_H_ /** * @file oskar_convert_relative_directions_to_enu_directions_cuda.h */ #include <oskar_global.h> #ifdef __cplusplus extern "C" { #endif /** * @brief * Converts from relative direction cosines to horizon ENU direction cosines * (single precision, GPU version). * * @details * This function transforms the given \f$(l, m, n)\f$ directions in the * equatorial frame to \f$(x, y, z)\f$ directions in the horizontal frame. * * It is equivalent to the product of matrix transformations as follows: * \f[ \begin{bmatrix} x \\ y \\ z \end{bmatrix} = R_x(\phi) \cdot R_y(-H_0) \cdot R_x(-\delta_0) \cdot \begin{bmatrix} l \\ m \\ n \end{bmatrix} \f] * * Here, \f$ R_x \f$ and \f$ R_y \f$ correspond to rotations around * the \f$x\f$- and \f$y\f$-axes, respectively. * The angles \f$ \phi \f$, \f$ H_0 \f$ and \f$ \delta_0 \f$ correspond to * the observer's geodetic latitude, the hour angle and the declination of * the phase centre. * * @param[out] x ENU direction cosines (East). * @param[out] y ENU direction cosines (North). * @param[out] z ENU direction cosines (up). * @param[in] num_points Number of points to convert. * @param[in] l Relative direction cosines. * @param[in] m Relative direction cosines. * @param[in] n Relative direction cosines. * @param[in] ha0 Hour angle of the origin of the relative directions, * in radians. * @param[in] dec0 Declination of the origin of the relative directions, * in radians. * @param[in] lat Latitude of the ENU coordinate frame, in radians. */ OSKAR_EXPORT void oskar_convert_relative_directions_to_enu_directions_cuda_f( float* x, float* y, float* z, int num_points, const float* l, const float* m, const float* n, float ha0, float dec0, float lat); /** * @brief * Converts from relative direction cosines to horizon ENU direction cosines * (double precision, GPU version). * * @details * This function transforms the given \f$(l, m, n)\f$ directions in the * equatorial frame to \f$(x, y, z)\f$ directions in the horizontal frame. * * It is equivalent to the product of matrix transformations as follows: * \f[ \begin{bmatrix} x \\ y \\ z \end{bmatrix} = R_x(\phi) \cdot R_y(-H_0) \cdot R_x(-\delta_0) \cdot \begin{bmatrix} l \\ m \\ n \end{bmatrix} \f] * * Here, \f$ R_x \f$ and \f$ R_y \f$ correspond to rotations around * the \f$x\f$- and \f$y\f$-axes, respectively. * The angles \f$ \phi \f$, \f$ H_0 \f$ and \f$ \delta_0 \f$ correspond to * the observer's geodetic latitude, the hour angle and the declination of * the phase centre. * * @param[out] x ENU direction cosines (East). * @param[out] y ENU direction cosines (North). * @param[out] z ENU direction cosines (up). * @param[in] num_points Number of points to convert. * @param[in] l Relative direction cosines. * @param[in] m Relative direction cosines. * @param[in] n Relative direction cosines. * @param[in] ha0 Hour angle of the origin of the relative directions, * in radians. * @param[in] dec0 Declination of the origin of the relative directions, * in radians. * @param[in] lat Latitude of the ENU coordinate frame, in radians. */ OSKAR_EXPORT void oskar_convert_relative_directions_to_enu_directions_cuda_d( double* x, double* y, double* z, int num_points, const double* l, const double* m, const double* n, double ha0, double dec0, double lat); #ifdef __CUDACC__ /* Kernels. */ __global__ void oskar_convert_relative_directions_to_enu_directions_cudak_f( float* __restrict__ x, float* __restrict__ y, float* __restrict__ z, const int num_points, const float* __restrict__ l, const float* __restrict__ m, const float* __restrict__ n, const float cos_ha0, const float sin_ha0, const float cos_dec0, const float sin_dec0, const float cos_lat, const float sin_lat); __global__ void oskar_convert_relative_directions_to_enu_directions_cudak_d( double* __restrict__ x, double* __restrict__ y, double* __restrict__ z, const int num_points, const double* __restrict__ l, const double* __restrict__ m, const double* __restrict__ n, const double cos_ha0, const double sin_ha0, const double cos_dec0, const double sin_dec0, const double cos_lat, const double sin_lat); #endif /* __CUDACC__ */ #ifdef __cplusplus } #endif #endif /* OSKAR_CONVERT_RELATIVE_DIRECTIONS_TO_ENU_DIRECTIONS_CUDA_H_ */
davepallot/OSKAR
oskar/telescope/station/oskar_evaluate_station_beam_aperture_array.h
/* * Copyright (c) 2012-2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef OSKAR_EVALUATE_STATION_BEAM_APERTURE_ARRAY_H_ #define OSKAR_EVALUATE_STATION_BEAM_APERTURE_ARRAY_H_ /** * @file oskar_evaluate_station_beam_aperture_array.h */ #include <oskar_global.h> #include <mem/oskar_mem.h> #include <telescope/station/oskar_station.h> #ifdef __cplusplus extern "C" { #endif /** * @brief * Evaluates the station beam for an aperture array station. * * @details * This function evaluates the beam for an aperture array station, * including any possible child stations and/or element models within the * station. * * The supplied station model must be fully set-up, and must include * the element positions, the required beam coordinates, and all associated * options and meta-data necessary for beamforming. * * The beam is evaluated at points defined by the horizontal Cartesian * direction cosines given in the vectors x,y,z. The output \p beam array * must be of the correct data type for the station element: * for example, it is an error to use polarised elements and supply a scalar * type for the output data. If in doubt, an output type of complex matrix will * always be acceptable, although it may be significantly slower for isotropic * elements. * * The work structure holds pointers to memory blocks used by routines which * are called by this wrapper. The structure must be initialised, but may be * empty. In this case, the internal buffers will be resized to the correct * dimensions on first use. Subsequent calls to this function should ideally * re-use the same work structure to ensure optimum performance and no needless * memory reallocation. * * @param[out] beam Station beam evaluated at x,y,z positions. * @param[in] station Fully populated station model structure. * @param[in] num_points Number of coordinates at which to evaluate * the beam. * @param[in] x Array of horizontal x coordinates at which to * evaluate the beam. * @param[in] y Array of horizontal y coordinates at which to * evaluate the beam. * @param[in] z Array of horizontal z coordinates at which to * evaluate the beam. * @param[in] gast The Greenwich Apparent Sidereal Time in radians. * @param[in] frequency_hz The observing frequency, in Hz. * @param[in] work Initialised structure containing temporary work * buffers. * @param[in] time_index Simulation time index. * @param[in,out] status Status return code. */ OSKAR_EXPORT void oskar_evaluate_station_beam_aperture_array(oskar_Mem* beam, const oskar_Station* station, int num_points, const oskar_Mem* x, const oskar_Mem* y, const oskar_Mem* z, double gast, double frequency_hz, oskar_StationWork* work, int time_index, int* status); #ifdef __cplusplus } #endif #endif /* OSKAR_EVALUATE_STATION_BEAM_APERTURE_ARRAY_H_ */
davepallot/OSKAR
oskar/math/src/oskar_evaluate_image_lon_lat_grid.c
/* * Copyright (c) 2013-2016, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "convert/oskar_convert_relative_directions_to_lon_lat.h" #include "math/oskar_evaluate_image_lm_grid.h" #include "math/oskar_evaluate_image_lon_lat_grid.h" #include "mem/oskar_mem.h" #include <stdlib.h> #ifdef __cplusplus extern "C" { #endif void oskar_evaluate_image_lon_lat_grid(oskar_Mem* lon, oskar_Mem* lat, int image_size_l, int image_size_m, double fov_rad_lon, double fov_rad_lat, double lon_rad, double lat_rad, int* status) { int num_pixels, type, location; oskar_Mem *lon_cpu, *lat_cpu; /* Get meta-data. */ type = oskar_mem_type(lon); location = oskar_mem_location(lon); num_pixels = image_size_l * image_size_m; /* Ensure enough space in output arrays. */ if ((int)oskar_mem_length(lon) < num_pixels) oskar_mem_realloc(lon, num_pixels, status); if ((int)oskar_mem_length(lat) < num_pixels) oskar_mem_realloc(lat, num_pixels, status); /* Check if safe to proceed. */ if (*status) return; /* Initialise temporary memory. */ if (location != OSKAR_CPU) { lon_cpu = oskar_mem_create(type, OSKAR_CPU, num_pixels, status); lat_cpu = oskar_mem_create(type, OSKAR_CPU, num_pixels, status); } else { lon_cpu = oskar_mem_create_alias(lon, 0, num_pixels, status); lat_cpu = oskar_mem_create_alias(lat, 0, num_pixels, status); } /* Check if safe to proceed. */ if (! *status) { /* Evaluate pixel grid and convert to longitude, latitude values. */ if (type == OSKAR_SINGLE) { oskar_evaluate_image_lm_grid_f(image_size_l, image_size_m, fov_rad_lon, fov_rad_lat, oskar_mem_float(lon_cpu, status), oskar_mem_float(lat_cpu, status)); oskar_convert_relative_directions_to_lon_lat_2d_f(num_pixels, oskar_mem_float_const(lon_cpu, status), oskar_mem_float_const(lat_cpu, status), lon_rad, lat_rad, oskar_mem_float(lon_cpu, status), oskar_mem_float(lat_cpu, status)); } else if (type == OSKAR_DOUBLE) { oskar_evaluate_image_lm_grid_d(image_size_l, image_size_m, fov_rad_lon, fov_rad_lat, oskar_mem_double(lon_cpu, status), oskar_mem_double(lat_cpu, status)); oskar_convert_relative_directions_to_lon_lat_2d_d(num_pixels, oskar_mem_double_const(lon_cpu, status), oskar_mem_double_const(lat_cpu, status), lon_rad, lat_rad, oskar_mem_double(lon_cpu, status), oskar_mem_double(lat_cpu, status)); } } /* Copy data to GPU and free temporary arrays, if required. */ if (location != OSKAR_CPU) { oskar_mem_copy(lon, lon_cpu, status); oskar_mem_copy(lat, lat_cpu, status); } oskar_mem_free(lon_cpu, status); oskar_mem_free(lat_cpu, status); } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/sky/src/oskar_update_horizon_mask.c
/* * Copyright (c) 2013-2018, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "sky/oskar_update_horizon_mask.h" #include "sky/oskar_update_horizon_mask_cuda.h" #include "utility/oskar_cl_utils.h" #include "utility/oskar_device_utils.h" #include <math.h> #ifdef __cplusplus extern "C" { #endif void oskar_update_horizon_mask(int num_sources, const oskar_Mem* l, const oskar_Mem* m, const oskar_Mem* n, const double ha0_rad, const double dec0_rad, const double lat_rad, oskar_Mem* mask, int* status) { int i, type, location, *mask_; double cos_ha0, sin_dec0, cos_dec0, sin_lat, cos_lat; double ll, mm, nn; if (*status) return; type = oskar_mem_precision(l); location = oskar_mem_location(mask); mask_ = oskar_mem_int(mask, status); cos_ha0 = cos(ha0_rad); sin_dec0 = sin(dec0_rad); cos_dec0 = cos(dec0_rad); sin_lat = sin(lat_rad); cos_lat = cos(lat_rad); ll = cos_lat * sin(ha0_rad); mm = sin_lat * cos_dec0 - cos_lat * cos_ha0 * sin_dec0; nn = sin_lat * sin_dec0 + cos_lat * cos_ha0 * cos_dec0; if (location == OSKAR_CPU) { if (type == OSKAR_SINGLE) { float ll_, mm_, nn_; const float *l_, *m_, *n_; l_ = oskar_mem_float_const(l, status); m_ = oskar_mem_float_const(m, status); n_ = oskar_mem_float_const(n, status); ll_ = (float) ll; mm_ = (float) mm; nn_ = (float) nn; for (i = 0; i < num_sources; ++i) mask_[i] |= ((l_[i] * ll_ + m_[i] * mm_ + n_[i] * nn_) > 0.f); } else if (type == OSKAR_DOUBLE) { const double *l_, *m_, *n_; l_ = oskar_mem_double_const(l, status); m_ = oskar_mem_double_const(m, status); n_ = oskar_mem_double_const(n, status); for (i = 0; i < num_sources; ++i) mask_[i] |= ((l_[i] * ll + m_[i] * mm + n_[i] * nn) > 0.); } else *status = OSKAR_ERR_BAD_DATA_TYPE; } else if (location == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA if (type == OSKAR_SINGLE) oskar_update_horizon_mask_cuda_f(num_sources, oskar_mem_float_const(l, status), oskar_mem_float_const(m, status), oskar_mem_float_const(n, status), (float) ll, (float) mm, (float) nn, mask_); else if (type == OSKAR_DOUBLE) oskar_update_horizon_mask_cuda_d(num_sources, oskar_mem_double_const(l, status), oskar_mem_double_const(m, status), oskar_mem_double_const(n, status), ll, mm, nn, mask_); else *status = OSKAR_ERR_BAD_DATA_TYPE; oskar_device_check_error(status); #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } else if (location & OSKAR_CL) { #ifdef OSKAR_HAVE_OPENCL cl_event event; cl_kernel k = 0; cl_int error, num; cl_uint arg = 0; size_t global_size, local_size; if (type == OSKAR_DOUBLE) k = oskar_cl_kernel("update_horizon_mask_double"); else if (type == OSKAR_SINGLE) k = oskar_cl_kernel("update_horizon_mask_float"); else { *status = OSKAR_ERR_BAD_DATA_TYPE; return; } if (!k) { *status = OSKAR_ERR_FUNCTION_NOT_AVAILABLE; return; } /* Set kernel arguments. */ num = (cl_int) num_sources; error = clSetKernelArg(k, arg++, sizeof(cl_int), &num); error |= clSetKernelArg(k, arg++, sizeof(cl_mem), oskar_mem_cl_buffer_const(l, status)); error |= clSetKernelArg(k, arg++, sizeof(cl_mem), oskar_mem_cl_buffer_const(m, status)); error |= clSetKernelArg(k, arg++, sizeof(cl_mem), oskar_mem_cl_buffer_const(n, status)); if (type == OSKAR_SINGLE) { const cl_float ll_ = (cl_float) ll; const cl_float mm_ = (cl_float) mm; const cl_float nn_ = (cl_float) nn; error |= clSetKernelArg(k, arg++, sizeof(cl_float), &ll_); error |= clSetKernelArg(k, arg++, sizeof(cl_float), &mm_); error |= clSetKernelArg(k, arg++, sizeof(cl_float), &nn_); } else if (type == OSKAR_DOUBLE) { const cl_double ll_ = (cl_double) ll; const cl_double mm_ = (cl_double) mm; const cl_double nn_ = (cl_double) nn; error |= clSetKernelArg(k, arg++, sizeof(cl_double), &ll_); error |= clSetKernelArg(k, arg++, sizeof(cl_double), &mm_); error |= clSetKernelArg(k, arg++, sizeof(cl_double), &nn_); } error |= clSetKernelArg(k, arg++, sizeof(cl_mem), oskar_mem_cl_buffer(mask, status)); if (error != CL_SUCCESS) { *status = OSKAR_ERR_INVALID_ARGUMENT; return; } /* Launch kernel on current command queue. */ local_size = oskar_cl_is_gpu() ? 256 : 128; global_size = ((num + local_size - 1) / local_size) * local_size; error = clEnqueueNDRangeKernel(oskar_cl_command_queue(), k, 1, NULL, &global_size, &local_size, 0, NULL, &event); if (error != CL_SUCCESS) *status = OSKAR_ERR_KERNEL_LAUNCH_FAILURE; #else *status = OSKAR_ERR_OPENCL_NOT_AVAILABLE; #endif } else *status = OSKAR_ERR_BAD_LOCATION; } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/telescope/station/src/oskar_station_save_layout.c
/* * Copyright (c) 2014-2015, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "telescope/station/private_station.h" #include "telescope/station/oskar_station.h" #include <stdio.h> #include <stdlib.h> #ifdef __cplusplus extern "C" { #endif void oskar_station_save_layout(const char* filename, const oskar_Station* station, int* status) { int i, location, type, num_elements; FILE* file; const oskar_Mem *x_weights, *y_weights, *z_weights; const oskar_Mem *x_signal, *y_signal, *z_signal; /* Check if safe to proceed. */ if (*status) return; /* Check type and location. */ type = oskar_station_precision(station); location = oskar_station_mem_location(station); num_elements = oskar_station_num_elements(station); if (type != OSKAR_SINGLE && type != OSKAR_DOUBLE) { *status = OSKAR_ERR_BAD_DATA_TYPE; return; } if (location != OSKAR_CPU) { *status = OSKAR_ERR_BAD_LOCATION; return; } /* Get pointers to the arrays. */ x_weights = oskar_station_element_measured_x_enu_metres_const(station); y_weights = oskar_station_element_measured_y_enu_metres_const(station); z_weights = oskar_station_element_measured_z_enu_metres_const(station); x_signal = oskar_station_element_true_x_enu_metres_const(station); y_signal = oskar_station_element_true_y_enu_metres_const(station); z_signal = oskar_station_element_true_z_enu_metres_const(station); /* Open the file. */ file = fopen(filename, "w"); if (!file) { *status = OSKAR_ERR_FILE_IO; return; } /* Save the station data. */ fprintf(file, "# Number of elements = %i\n", num_elements); fprintf(file, "# Longitude [radians] = %f\n", station->lon_rad); fprintf(file, "# Latitude [radians] = %f\n", station->lat_rad); fprintf(file, "# Altitude [metres] = %f\n", station->alt_metres); fprintf(file, "# Local horizontal x (east), y (north), z (up) [metres], " "delta x, delta y, delta z [metres]\n"); if (type == OSKAR_DOUBLE) { const double *x_, *y_, *z_, *xs_, *ys_, *zs_; x_ = oskar_mem_double_const(x_weights, status); y_ = oskar_mem_double_const(y_weights, status); z_ = oskar_mem_double_const(z_weights, status); xs_ = oskar_mem_double_const(x_signal, status); ys_ = oskar_mem_double_const(y_signal, status); zs_ = oskar_mem_double_const(z_signal, status); for (i = 0; i < num_elements; ++i) { double x, y, z; x = x_[i]; y = y_[i]; z = z_[i]; fprintf(file, "% 14.6f % 14.6f % 14.6f % 14.6f % 14.6f % 14.6f\n", x, y, z, (xs_[i] - x), (ys_[i] - y), (zs_[i] - z)); } } else if (type == OSKAR_SINGLE) { const float *x_, *y_, *z_, *xs_, *ys_, *zs_; x_ = oskar_mem_float_const(x_weights, status); y_ = oskar_mem_float_const(y_weights, status); z_ = oskar_mem_float_const(z_weights, status); xs_ = oskar_mem_float_const(x_signal, status); ys_ = oskar_mem_float_const(y_signal, status); zs_ = oskar_mem_float_const(z_signal, status); for (i = 0; i < num_elements; ++i) { float x, y, z; x = x_[i]; y = y_[i]; z = z_[i]; fprintf(file, "% 14.6f % 14.6f % 14.6f % 14.6f % 14.6f % 14.6f\n", x, y, z, (xs_[i] - x), (ys_[i] - y), (zs_[i] - z)); } } /* Close the file. */ fclose(file); } #ifdef __cplusplus } #endif
davepallot/OSKAR
oskar/mem/src/oskar_mem_scale_real.c
/* * Copyright (c) 2011-2017, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "mem/oskar_mem.h" #include "mem/oskar_mem_scale_real_cuda.h" #include "mem/private_mem.h" #include "utility/oskar_cl_utils.h" #include "utility/oskar_device_utils.h" #ifdef __cplusplus extern "C" #endif void oskar_mem_scale_real(oskar_Mem* mem, double value, int* status) { size_t num_elements, i; #ifdef OSKAR_HAVE_OPENCL cl_kernel k = 0; #endif /* Check if safe to proceed. */ if (*status) return; /* Get memory meta-data. */ num_elements = mem->num_elements; /* Check if elements are real, complex or matrix. */ if (oskar_type_is_complex(mem->type)) num_elements *= 2; if (oskar_type_is_matrix(mem->type)) num_elements *= 4; /* Scale the vector. */ if (oskar_type_is_single(mem->type)) { if (mem->location == OSKAR_CPU) { float *aa; aa = (float*) mem->data; for (i = 0; i < num_elements; ++i) aa[i] *= (float)value; } else if (mem->location == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA oskar_mem_scale_real_cuda_f((int)num_elements, (float)value, (float*)(mem->data)); oskar_device_check_error(status); #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } else if (mem->location & OSKAR_CL) { #ifdef OSKAR_HAVE_OPENCL k = oskar_cl_kernel("mem_scale_float"); #else *status = OSKAR_ERR_OPENCL_NOT_AVAILABLE; #endif } else *status = OSKAR_ERR_BAD_LOCATION; } else if (oskar_type_is_double(mem->type)) { if (mem->location == OSKAR_CPU) { double *aa; aa = (double*) mem->data; for (i = 0; i < num_elements; ++i) aa[i] *= value; } else if (mem->location == OSKAR_GPU) { #ifdef OSKAR_HAVE_CUDA oskar_mem_scale_real_cuda_d((int)num_elements, value, (double*)(mem->data)); oskar_device_check_error(status); #else *status = OSKAR_ERR_CUDA_NOT_AVAILABLE; #endif } else if (mem->location & OSKAR_CL) { #ifdef OSKAR_HAVE_OPENCL k = oskar_cl_kernel("mem_scale_double"); #else *status = OSKAR_ERR_OPENCL_NOT_AVAILABLE; #endif } else *status = OSKAR_ERR_BAD_LOCATION; } else { *status = OSKAR_ERR_BAD_DATA_TYPE; } #ifdef OSKAR_HAVE_OPENCL /* Call OpenCL kernel if required. */ if ((mem->location & OSKAR_CL) && !*status) { if (k) { cl_device_type dev_type; cl_int error, gpu, n; size_t global_size, local_size; /* Set kernel arguments. */ clGetDeviceInfo(oskar_cl_device_id(), CL_DEVICE_TYPE, sizeof(cl_device_type), &dev_type, NULL); gpu = dev_type & CL_DEVICE_TYPE_GPU; n = (cl_int) num_elements; error = clSetKernelArg(k, 0, sizeof(cl_int), &n); if (oskar_type_is_double(mem->type)) { cl_double v = (cl_double) value; error |= clSetKernelArg(k, 1, sizeof(cl_double), &v); } else { cl_float v = (cl_float) value; error |= clSetKernelArg(k, 1, sizeof(cl_float), &v); } error |= clSetKernelArg(k, 2, sizeof(cl_mem), oskar_mem_cl_buffer(mem, status)); if (*status) return; if (error != CL_SUCCESS) { *status = OSKAR_ERR_INVALID_ARGUMENT; return; } /* Launch kernel on current command queue. */ local_size = gpu ? 256 : 128; global_size = ((num_elements + local_size - 1) / local_size) * local_size; error = clEnqueueNDRangeKernel(oskar_cl_command_queue(), k, 1, NULL, &global_size, &local_size, 0, NULL, NULL); if (error != CL_SUCCESS) { *status = OSKAR_ERR_KERNEL_LAUNCH_FAILURE; return; } } else { *status = OSKAR_ERR_FUNCTION_NOT_AVAILABLE; } } #endif }
davepallot/OSKAR
oskar/beam_pattern/src/oskar_beam_pattern_reset_cache.c
/* * Copyright (c) 2016, The University of Oxford * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of the University of Oxford nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "beam_pattern/oskar_beam_pattern.h" #include "beam_pattern/private_beam_pattern.h" #include "beam_pattern/private_beam_pattern_free_device_data.h" #include <stdlib.h> #include <stdio.h> #include <fitsio.h> #ifdef __cplusplus extern "C" { #endif void oskar_beam_pattern_reset_cache(oskar_BeamPattern* h, int* status) { int i; oskar_beam_pattern_free_device_data(h, status); oskar_mem_free(h->x, status); oskar_mem_free(h->y, status); oskar_mem_free(h->z, status); oskar_mem_free(h->pix, status); oskar_mem_free(h->ctemp, status); h->x = h->y = h->z = h->pix = h->ctemp = NULL; /* Close files and free data products. */ for (i = 0; i < h->num_data_products; ++i) { if (h->data_products[i].text_file) fclose(h->data_products[i].text_file); if (h->data_products[i].fits_file) ffclos(h->data_products[i].fits_file, status); } free(h->data_products); h->data_products = NULL; h->num_data_products = 0; } #ifdef __cplusplus } #endif