forked from skystar0227/CUMP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.c
More file actions
178 lines (140 loc) · 5.59 KB
/
memory.c
File metadata and controls
178 lines (140 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* Memory allocation routines.
Copyright 2012 Takatoshi Nakayama.
This file is part of the CUMP Library.
The CUMP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
The CUMP Library 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the CUMP Library. If not, see http://www.gnu.org/licenses/. */
#if defined (_DEBUG) || defined (DEBUG)
#include <stdio.h>
#endif
#include <cuda_runtime_api.h>
#include "cump.h"
#include "cump-impl.h"
void* __cump_default_allocate (size_t);
void* __cump_default_allocate_2D (size_t*, size_t, size_t);
/*void* __cump_default_reallocate (void*, size_t, size_t);*/
void __cump_default_free (void*, size_t);
void __cump_default_memcpy (void*, void const *, size_t);
void __cump_default_memcpy_h2d (void*, void const *, size_t);
void __cump_default_memcpy_d2h (void*, void const *, size_t);
void __cump_default_memcpy_d2d (void*, void const *, size_t);
void __cump_default_memcpy_2D (void*, size_t, void const *, size_t, size_t, size_t);
void __cump_default_memcpy_2D_h2d (void*, size_t, void const *, size_t, size_t, size_t);
void __cump_default_memcpy_2D_d2h (void*, size_t, void const *, size_t, size_t, size_t);
void __cump_default_memcpy_2D_d2d (void*, size_t, void const *, size_t, size_t, size_t);
void* (*__cump_allocate_func) (size_t)
= __cump_default_allocate;
void* (*__cump_allocate_2D_func) (size_t*, size_t, size_t)
= __cump_default_allocate_2D;
/*
void* (*__cump_reallocate_func) (void*, size_t, size_t)
= __cump_default_reallocate;
*/
void (*__cump_free_func) (void*, size_t)
= __cump_default_free;
void (*__cump_memcpy_func) (void*, void const *, size_t)
= __cump_default_memcpy;
void (*__cump_memcpy_h2d_func) (void*, void const *, size_t)
= __cump_default_memcpy_h2d;
void (*__cump_memcpy_d2h_func) (void*, void const *, size_t)
= __cump_default_memcpy_d2h;
void (*__cump_memcpy_d2d_func) (void*, void const *, size_t)
= __cump_default_memcpy_d2d;
void (*__cump_memcpy_2D_func) (void*, size_t, void const *, size_t, size_t, size_t)
= __cump_default_memcpy_2D;
void (*__cump_memcpy_2D_h2d_func) (void*, size_t, void const *, size_t, size_t, size_t)
= __cump_default_memcpy_2D_h2d;
void (*__cump_memcpy_2D_d2h_func) (void*, size_t, void const *, size_t, size_t, size_t)
= __cump_default_memcpy_2D_d2h;
void (*__cump_memcpy_2D_d2d_func) (void*, size_t, void const *, size_t, size_t, size_t)
= __cump_default_memcpy_2D_d2d;
#if defined (_DEBUG) || defined (DEBUG)
#define SAFE_CALL(call) \
do \
{ \
cudaError_t e = call; \
if (e != cudaSuccess) \
{ \
fprintf \
( stderr, "Device memory error (%s): %s.\n" \
, __func__, cudaGetErrorString (e) \
); \
exit (EXIT_FAILURE); \
} \
} \
while(0)
#else
#define SAFE_CALL(call) (call)
#endif
void* __cump_default_allocate (size_t size)
{
void *ret;
SAFE_CALL (cudaMalloc (&ret, size));
return ret;
}
void* __cump_default_allocate_2D (size_t *pitch, size_t width, size_t height)
{
void *ret;
SAFE_CALL (cudaMallocPitch (&ret, pitch, width, height));
return ret;
}
/*void* __cump_default_reallocate (void *oldptr, size_t old_size, size_t new_size)
{
void *ret = 0;
return ret;
}*/
void __cump_default_free (void *blk_ptr, size_t blk_size)
{
SAFE_CALL (cudaFree (blk_ptr));
}
void __cump_default_memcpy (void *dst, void const *src, size_t size)
{
SAFE_CALL (cudaMemcpy (dst, src, size, cudaMemcpyDefault));
}
void __cump_default_memcpy_h2d (void *dst, void const *src, size_t size)
{
SAFE_CALL (cudaMemcpy (dst, src, size, cudaMemcpyHostToDevice));
}
void __cump_default_memcpy_d2h (void *dst, void const *src, size_t size)
{
SAFE_CALL (cudaMemcpy (dst, src, size, cudaMemcpyDeviceToHost));
}
void __cump_default_memcpy_d2d (void *dst, void const *src, size_t size)
{
SAFE_CALL (cudaMemcpy (dst, src, size, cudaMemcpyDeviceToDevice));
}
void __cump_default_memcpy_2D
( void *dst, size_t dpitch, void const *src, size_t spitch
, size_t width, size_t height
)
{
SAFE_CALL (cudaMemcpy2D (dst, dpitch, src, spitch, width, height, cudaMemcpyDefault));
}
void __cump_default_memcpy_2D_h2d
( void *dst, size_t dpitch, void const *src, size_t spitch
, size_t width, size_t height
)
{
SAFE_CALL (cudaMemcpy2D (dst, dpitch, src, spitch, width, height, cudaMemcpyHostToDevice));
}
void __cump_default_memcpy_2D_d2h
( void *dst, size_t dpitch, void const *src, size_t spitch
, size_t width, size_t height
)
{
SAFE_CALL (cudaMemcpy2D (dst, dpitch, src, spitch, width, height, cudaMemcpyDeviceToHost));
}
void __cump_default_memcpy_2D_d2d
( void *dst, size_t dpitch, void const *src, size_t spitch
, size_t width, size_t height
)
{
SAFE_CALL (cudaMemcpy2D (dst, dpitch, src, spitch, width, height, cudaMemcpyDeviceToDevice));
}