Skip to content
Snippets Groups Projects
Commit da4b365d authored by Marcel Loose's avatar Marcel Loose :sunglasses:
Browse files

Task #4362: First, very preliminary version of complex number class for CUDA.

parent 146af842
Branches
Tags
No related merge requests found
//# 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
//# 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
...@@ -6,6 +6,8 @@ include(LofarCTest) ...@@ -6,6 +6,8 @@ include(LofarCTest)
# source files and headers in subdirectories include UnitTest.h # source files and headers in subdirectories include UnitTest.h
include_directories(${CMAKE_CURRENT_SOURCE_DIR}) include_directories(${CMAKE_CURRENT_SOURCE_DIR})
lofar_add_test(t_complex t_complex.cc)
lofar_add_test(tBestEffortQueue tBestEffortQueue.cc) lofar_add_test(tBestEffortQueue tBestEffortQueue.cc)
lofar_add_test(tContext tContext.cc) lofar_add_test(tContext tContext.cc)
lofar_add_test(tPerformanceCounter tPerformanceCounter.cc) lofar_add_test(tPerformanceCounter tPerformanceCounter.cc)
......
lofar_add_test(tCUDAException tCUDAException.cc) 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)
#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;
}
#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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment