CudaFloatTexture1D.cpp
上传用户:chinafayin
上传日期:2022-04-05
资源大小:153k
文件大小:2k
源码类别:

并行计算

开发平台:

Visual C++

  1. /*
  2.     FastGrid (formerly AutoGrid)
  3.     Copyright (C) 2009 The Scripps Research Institute. All rights reserved.
  4.     Copyright (C) 2009 Masaryk University. All rights reserved.
  5.     AutoGrid is a Trade Mark of The Scripps Research Institute.
  6.     This program is free software; you can redistribute it and/or
  7.     modify it under the terms of the GNU General Public License
  8.     as published by the Free Software Foundation; either version 2
  9.     of the License, or (at your option) any later version.
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  17. */
  18. #include <algorithm>
  19. #include "CudaFloatTexture1D.h"
  20. CudaFloatTexture1D::CudaFloatTexture1D(int width, const double *data, CudaAction action, cudaStream_t stream, CudaInternalAPI *api)
  21. {
  22.     channelDesc = cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat);
  23.     // Allocate the texture on the GPU...
  24.     CUDA_SAFE_CALL(cudaMallocArray(&deviceArray, &channelDesc, width, 1));
  25.     // ... and in page-locked system memory
  26.     CUDA_SAFE_CALL(cudaMallocHost((void**)&hostMem, sizeof(float) * width));
  27.     // Convert doubles to floats and save them to page-locked system memory
  28.     std::transform(data, data + width, hostMem, typecast<float, double>);
  29.     // Copy floats from the page-locked memory to the GPU
  30.     CUDA_SAFE_CALL(cudaMemcpyToArrayAsync(deviceArray, 0, 0, hostMem, sizeof(float) * width, cudaMemcpyHostToDevice, stream));
  31.     if (action == BindToKernel)
  32.         api->setDistDepDielTexture(deviceArray, &channelDesc);
  33. }
  34. CudaFloatTexture1D::~CudaFloatTexture1D()
  35. {
  36.     CUDA_SAFE_CALL(cudaFreeArray(deviceArray));
  37.     CUDA_SAFE_CALL(cudaFreeHost(hostMem));
  38. }