2022-08-09 18:53:53 +02:00
|
|
|
/*
|
2022-09-05 10:39:42 +02:00
|
|
|
* Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
|
|
|
|
* All rights reserved. This file is part of MD-Bench.
|
|
|
|
* Use of this source code is governed by a LGPL-3.0
|
|
|
|
* license that can be found in the LICENSE file.
|
2022-08-09 18:53:53 +02:00
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
2022-08-12 18:12:29 +02:00
|
|
|
#include <stdlib.h>
|
2022-08-09 18:53:53 +02:00
|
|
|
//---
|
2022-08-12 18:12:29 +02:00
|
|
|
#include <device.h>
|
|
|
|
|
|
|
|
#ifdef CUDA_TARGET
|
|
|
|
#include <cuda_runtime.h>
|
|
|
|
|
|
|
|
void cuda_assert(const char *label, cudaError_t err) {
|
|
|
|
if (err != cudaSuccess) {
|
|
|
|
printf("[CUDA Error]: %s: %s\r\n", label, cudaGetErrorString(err));
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *allocateGPU(size_t bytesize) {
|
|
|
|
void *ptr;
|
|
|
|
#ifdef CUDA_HOST_MEMORY
|
|
|
|
cuda_assert("allocateGPU", cudaMallocHost((void **) &ptr, bytesize));
|
|
|
|
#else
|
|
|
|
cuda_assert("allocateGPU", cudaMalloc((void **) &ptr, bytesize));
|
|
|
|
#endif
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Data is not preserved
|
|
|
|
void *reallocateGPU(void *ptr, size_t new_bytesize) {
|
|
|
|
if(ptr != NULL) {
|
|
|
|
#ifdef CUDA_HOST_MEMORY
|
|
|
|
cudaFreeHost(ptr);
|
|
|
|
#else
|
|
|
|
cudaFree(ptr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return allocateGPU(new_bytesize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void memcpyToGPU(void *d_ptr, void *h_ptr, size_t bytesize) {
|
|
|
|
#ifndef CUDA_HOST_MEMORY
|
|
|
|
cuda_assert("memcpyToGPU", cudaMemcpy(d_ptr, h_ptr, bytesize, cudaMemcpyHostToDevice));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void memcpyFromGPU(void *h_ptr, void *d_ptr, size_t bytesize) {
|
|
|
|
#ifndef CUDA_HOST_MEMORY
|
|
|
|
cuda_assert("memcpyFromGPU", cudaMemcpy(h_ptr, d_ptr, bytesize, cudaMemcpyDeviceToHost));
|
|
|
|
#endif
|
|
|
|
}
|
2022-08-09 18:53:53 +02:00
|
|
|
|
2022-08-12 18:12:29 +02:00
|
|
|
void memsetGPU(void *d_ptr, int value, size_t bytesize) {
|
|
|
|
cuda_assert("memsetGPU", cudaMemset(d_ptr, value, bytesize));
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
void initDevice(Atom *atom, Neighbor *neighbor) {}
|
|
|
|
void *allocateGPU(size_t bytesize) { return NULL; }
|
|
|
|
void *reallocateGPU(void *ptr, size_t new_bytesize) { return NULL; }
|
|
|
|
void memcpyToGPU(void *d_ptr, void *h_ptr, size_t bytesize) {}
|
|
|
|
void memcpyFromGPU(void *h_ptr, void *d_ptr, size_t bytesize) {}
|
|
|
|
void memsetGPU(void *d_ptr, int value, size_t bytesize) {}
|
|
|
|
#endif
|