Skip to content
Snippets Groups Projects
MemoryProcessing.cu 1.72 KiB
Newer Older
数学の武士's avatar
数学の武士 committed
#include "../include/MemoryProcessing.cuh"
#include <cuda.h>
#include <cuda_runtime_api.h>

数学の武士's avatar
数学の武士 committed
namespace memproc
数学の武士's avatar
数学の武士 committed
{
数学の武士's avatar
数学の武士 committed
    template<>
    bool dealloc<MemType::GPU>(void *&array, size_t &allocated_size)
数学の武士's avatar
数学の武士 committed
    {
数学の武士's avatar
数学の武士 committed
        if(allocated_size > 0)
        {
            cudaFree(array);
            allocated_size = 0;
        }
数学の武士's avatar
.  
数学の武士 committed

数学の武士's avatar
数学の武士 committed
        return true;
    }
数学の武士's avatar
数学の武士 committed

数学の武士's avatar
数学の武士 committed
    template<>
    bool dealloc<MemType::GPU>(void *&array)
    {
        cudaFree(array);
        return true;
    }
数学の武士's avatar
数学の武士 committed

数学の武士's avatar
数学の武士 committed
    template <>
    bool alloc<MemType::GPU>(void *&array, const size_t new_size)
数学の武士's avatar
数学の武士 committed
    {
        cudaMalloc ( (void **)&array, new_size);
        cudaMemset(array, 0, new_size);
数学の武士's avatar
数学の武士 committed

        return true;
数学の武士's avatar
数学の武士 committed
    template <>
    bool realloc<MemType::GPU>(void *&array, size_t &allocated_size, const size_t new_size)
    {
        if(new_size > allocated_size)
        {
            if(allocated_size > 0) dealloc<MemType::GPU>(array, allocated_size);
            allocated_size = new_size;
            cudaMalloc ( (void **)&array, new_size);
            cudaMemset(array, 0, new_size);
        }
数学の武士's avatar
数学の武士 committed

数学の武士's avatar
数学の武士 committed
        return true;
    }
数学の武士's avatar
数学の武士 committed

数学の武士's avatar
数学の武士 committed
    template <>
    bool memcopy<MemType::GPU, MemType::CPU>(void *dst, const void* src, const size_t copy_elem_size)
    {
        cudaMemcpy ( dst, src, copy_elem_size, cudaMemcpyHostToDevice);
数学の武士's avatar
数学の武士 committed

数学の武士's avatar
数学の武士 committed
        return true;
    }
数学の武士's avatar
数学の武士 committed

数学の武士's avatar
数学の武士 committed
    template <>
    bool memcopy<MemType::CPU, MemType::GPU>(void *dst, const void* src, const size_t copy_elem_size)
    {
        cudaMemcpy ( dst, src, copy_elem_size, cudaMemcpyDeviceToHost);
        return true;
    }

    template <>
    bool memcopy<MemType::GPU, MemType::GPU>(void *dst, const void* src, const size_t copy_elem_size)
    {
        cudaMemcpy ( dst, src, copy_elem_size, cudaMemcpyDeviceToDevice);
        return true;
    }
数学の武士's avatar
数学の武士 committed
}