Skip to content
Snippets Groups Projects
Commit bd775a10 authored by 数学の武士's avatar 数学の武士
Browse files

Initial commit

parents
Branches
No related tags found
No related merge requests found
{
"files.associations": {
"array": "cpp",
"cstddef": "cpp"
}
}
\ No newline at end of file
cmake_minimum_required(VERSION 3.0)
enable_language(CXX)
set(CMAKE_CXX_STANDARD 11)
option(INCLUDE_CUDA "GPU build in mode" OFF)
project(MemoryProcessing)
if(INCLUDE_CUDA)
enable_language(CUDA)
find_package(CUDA REQUIRED)
include_directories(${CUDA_INCLUDE_DIRS})
# -DCMAKE_CUDA_ARCHITECTURES=native -- Compile for the architecture(s) of the host's GPU(s)
endif(INCLUDE_CUDA)
set(SOURCES_CXX
src/MemoryProcessing.cpp
)
if(INCLUDE_CUDA)
set(SOURCES_CU
src/MemoryProcessing.cu
)
endif(INCLUDE_CUDA)
add_library(memproc STATIC ${SOURCES_CU} ${SOURCES_CXX})
\ No newline at end of file
#pragma once
#include "TemplateParameters.h"
#include <cstddef>
template <MemType memtype>
bool realloc(void *&array, size_t &allocated_size, const size_t new_size);
template<MemType memtype>
bool dealloc(void *&array, size_t &allocated_size);
template <MemType dst_memtype, MemType src_memtype>
bool memcopy(void *dst, const void* src, const size_t copy_elem_size);
\ No newline at end of file
#pragma once
#include "TemplateParameters.h"
#include <cstddef>
template <MemType memtype>
bool realloc(void *&array, size_t &allocated_size, const size_t new_size);
template<MemType memtype>
bool dealloc(void *&array, size_t &allocated_size);
template <MemType dst_memtype, MemType src_memtype>
bool memcopy(void *dst, const void* src, const size_t copy_elem_size);
#pragma once
enum MemType {CPU, GPU};
\ No newline at end of file
#include "../include/MemoryProcessing.h"
#include <cstdlib>
#include <cstring>
template<>
bool dealloc<MemType::CPU>(void *&array, size_t &allocated_size)
{
if(allocated_size > 0)
{
free(array);
allocated_size = 0;
}
return true;
}
template <>
bool realloc<MemType::CPU>(void *&array, size_t &allocated_size, const size_t new_size)
{
if(new_size > allocated_size)
{
if(allocated_size > 0) dealloc<MemType::CPU>(array, allocated_size);
allocated_size = new_size;
array = malloc(new_size);
memset(array, 0, new_size);
}
return true;
}
template <>
bool memcopy<MemType::CPU, MemType::CPU>(void *dst, const void* src, const size_t copy_elem_size)
{
memcpy(dst, src, copy_elem_size);
return true;
}
\ No newline at end of file
#include "../include/MemoryProcessing.cuh"
#include <cuda.h>
#include <cuda_runtime_api.h>
template<>
bool dealloc<MemType::GPU>(void *&array, size_t &allocated_size)
{
if(allocated_size > 0)
{
cudaFree(array);
allocated_size = 0;
}
return true;
}
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);
}
return true;
}
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);
return true;
}
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;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment