diff --git a/RTCP/Cobalt/GPUProc/src/complex.h b/RTCP/Cobalt/GPUProc/src/complex.h new file mode 100644 index 0000000000000000000000000000000000000000..a8668593bc5d21b0fe35455370407176eff07b62 --- /dev/null +++ b/RTCP/Cobalt/GPUProc/src/complex.h @@ -0,0 +1,83 @@ +//# complex.h: Support for complex numbers on GPUs. +//# +//# Copyright (C) 2012-2013 ASTRON (Netherlands Institute for Radio Astronomy) +//# P.O. Box 2, 7990 AA Dwingeloo, The Netherlands +//# +//# This file is part of the LOFAR software suite. +//# The LOFAR software suite is free software: you can redistribute it and/or +//# modify it under the terms of the GNU General Public License as published +//# by the Free Software Foundation, either version 3 of the License, or +//# (at your option) any later version. +//# +//# The LOFAR software suite is distributed in the hope that it will be useful, +//# but WITHOUT ANY WARRANTY; without even the implied warranty of +//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +//# GNU General Public License for more details. +//# +//# You should have received a copy of the GNU General Public License along +//# with the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>. +//# +//# $Id$ + +// \file +// Support for complex numbers on GPUs. + +#ifndef LOFAR_GPUPROC_COMPLEX_H +#define LOFAR_GPUPROC_COMPLEX_H + +// #if defined (USE_CUDA) && defined (USE_OPENCL) +// # error "Either CUDA or OpenCL must be enabled, not both" +// #endif + +//# Forward declaration +namespace LOFAR { + namespace Cobalt { + namespace gpu { + template<typename T> class complex; + // template<> class complex<float>; + // template<> class complex<double>; + } + } +} + +#include <complex> + +namespace LOFAR +{ + namespace Cobalt + { + namespace gpu + { + // Template to represent complex numbers on a GPU. + // The non-specialized version is based on std::complex. + // Specializations for float and double are included from a + // language-specific file (CUDA or OpenCL). + template<typename T> + class complex : public std::complex<T> + { + public: + // Default constructor. + // __host__ __device__ + complex(const T& re = T(), const T& im = T()) + : std::complex<T>(re, im) { } + + // Copy constructor. + template<typename U> + // __host__ __device__ + complex(const complex<U>& z) + : std::complex<T>(z.real(), z.imag()) { } + }; + + } // namespace gpu + + } // namespace Cobalt + +} // namespace LOFAR + +// #if defined (USE_CUDA) +// # include "cuda/complex.h" +// #elif defined (USE_OPENCL) +// # include "opencl/complex.h" +// #endif + +#endif diff --git a/RTCP/Cobalt/GPUProc/src/cuda/complex.h b/RTCP/Cobalt/GPUProc/src/cuda/complex.h new file mode 100644 index 0000000000000000000000000000000000000000..720261067dafd8c1b7108d5e944f4b6097bbb3c4 --- /dev/null +++ b/RTCP/Cobalt/GPUProc/src/cuda/complex.h @@ -0,0 +1,54 @@ +//# complex.h: Support for complex numbers in CUDA. +//# +//# Copyright (C) 2012-2013 ASTRON (Netherlands Institute for Radio Astronomy) +//# P.O. Box 2, 7990 AA Dwingeloo, The Netherlands +//# +//# This file is part of the LOFAR software suite. +//# The LOFAR software suite is free software: you can redistribute it and/or +//# modify it under the terms of the GNU General Public License as published +//# by the Free Software Foundation, either version 3 of the License, or +//# (at your option) any later version. +//# +//# The LOFAR software suite is distributed in the hope that it will be useful, +//# but WITHOUT ANY WARRANTY; without even the implied warranty of +//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +//# GNU General Public License for more details. +//# +//# You should have received a copy of the GNU General Public License along +//# with the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>. +//# +//# $Id$ + +#ifndef LOFAR_GPUPROC_CUDA_COMPLEX_H +#define LOFAR_GPUPROC_CUDA_COMPLEX_H + +// \file cuda/complex.h +// Support for complex numbers in CUDA. + +#include <cuComplex.h> + +namespace LOFAR +{ + namespace Cobalt + { + namespace gpu + { + // template<> class complex<float> : cuFloatComplex + // { + // // Default constructor. + // complex(float re = 0.0f, float im = 0.0f) + // : std::complex<T>(re, im) { } + // }; + + // template<> class complex<double> + // { + // }; + + + } // namespace gpu + + } // namespace Cobalt + +} // namespace LOFAR + +#endif diff --git a/RTCP/Cobalt/GPUProc/test/CMakeLists.txt b/RTCP/Cobalt/GPUProc/test/CMakeLists.txt index cd13d47b9faabf8f7d19e4b160eac5cdfee6ae36..c69daab0798a331ec98f2df1d8314ae4f2cb7975 100644 --- a/RTCP/Cobalt/GPUProc/test/CMakeLists.txt +++ b/RTCP/Cobalt/GPUProc/test/CMakeLists.txt @@ -6,6 +6,8 @@ include(LofarCTest) # source files and headers in subdirectories include UnitTest.h include_directories(${CMAKE_CURRENT_SOURCE_DIR}) +lofar_add_test(t_complex t_complex.cc) + lofar_add_test(tBestEffortQueue tBestEffortQueue.cc) lofar_add_test(tContext tContext.cc) lofar_add_test(tPerformanceCounter tPerformanceCounter.cc) diff --git a/RTCP/Cobalt/GPUProc/test/cuda/CMakeLists.txt b/RTCP/Cobalt/GPUProc/test/cuda/CMakeLists.txt index 10ab81e09db54e519eac26110696df9deebd8482..93aa214e1f79a80bc438ff506e58427fa8d4ddd3 100644 --- a/RTCP/Cobalt/GPUProc/test/cuda/CMakeLists.txt +++ b/RTCP/Cobalt/GPUProc/test/cuda/CMakeLists.txt @@ -1 +1,5 @@ lofar_add_test(tCUDAException tCUDAException.cc) +cuda_add_executable(t_kernel t_kernel.cu) +target_link_libraries(t_kernel ${${PACKAGE_NAME}_LINK_LIBRARIES}) +#add_test(t_kernel t_kernel) + diff --git a/RTCP/Cobalt/GPUProc/test/cuda/t_kernel.cu b/RTCP/Cobalt/GPUProc/test/cuda/t_kernel.cu new file mode 100644 index 0000000000000000000000000000000000000000..386d869d0683359e4ef5e4bf5098b2d7bff21fd5 --- /dev/null +++ b/RTCP/Cobalt/GPUProc/test/cuda/t_kernel.cu @@ -0,0 +1,50 @@ +#include <GPUProc/complex.h> +#include <GPUProc/cuda/CUDAException.h> +#include <iostream> + +using namespace LOFAR::Cobalt; + +__global__ void kernel() +{ + /* gpu::complex<float> cf(3.14f, 2.72f); */ + /* cf *= 3.0f; */ + /* gpu::complex<double> cd(1.618, 0.577); */ + /* cd *= 3.0; */ + /* gpu::complex<long double> cl(1.41421356237L, 1.73205080757L); */ + /* cl *= 3.0L; */ + /* std::cout << cf << std::endl; */ + /* std::cout << cd << std::endl; */ + /* std::cout << cl << std::endl; */ + /* cd *= cf; */ + /* cl *= cd; */ + /* cf *= cl; */ + /* std::cout << cf << std::endl; */ + /* std::cout << cd << std::endl; */ + /* std::cout << cl << std::endl; */ +} + +using namespace std; + +int main() +{ + std::cout << "Executing kernel" << std::endl; + kernel<<<1,1>>>(); + CUDA_CALL(cudaGetLastError()); + gpu::complex<float> cf(3.14f, 2.72f); + cf *= 3.0f; + gpu::complex<double> cd(1.618, 0.577); + cd *= 3.0; + gpu::complex<long double> cl(1.41421356237L, 1.73205080757L); + cl *= 3.0L; + std::cout << cf << std::endl; + std::cout << cd << std::endl; + std::cout << cl << std::endl; + cd *= cf; + cl *= cd; + cf *= cl; + std::cout << cf << std::endl; + std::cout << cd << std::endl; + std::cout << cl << std::endl; + std::cout << "Done" << std::endl; + return 0; +} diff --git a/RTCP/Cobalt/GPUProc/test/t_complex.cc b/RTCP/Cobalt/GPUProc/test/t_complex.cc new file mode 100644 index 0000000000000000000000000000000000000000..9dd56deffd7384736c605e56d5b4181e15a78311 --- /dev/null +++ b/RTCP/Cobalt/GPUProc/test/t_complex.cc @@ -0,0 +1,30 @@ +#include <iostream> + +#if 1 +#include <GPUProc/complex.h> +using namespace LOFAR::Cobalt; +#else +#include <complex> +namespace gpu = std; +#endif + +int main() +{ + gpu::complex<float> cf(3.14f, 2.72f); + cf *= 3; + gpu::complex<double> cd(1.618, 0.577); + cd *= 3; + gpu::complex<long double> cl(1.41421356237L, 1.73205080757L); + cl *= 3; + std::cout << cf << std::endl; + std::cout << cd << std::endl; + std::cout << cl << std::endl; + cd *= gpu::complex<double>(cf); + cf *= gpu::complex<float>(cd); + cl *= cd; + cf *= cl; + std::cout << cf << std::endl; + std::cout << cd << std::endl; + std::cout << cl << std::endl; + return 0; +}