File: nvd_memory.h

package info (click to toggle)
lammps 20220106.git7586adbb6a%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 348,064 kB
  • sloc: cpp: 831,421; python: 24,896; xml: 14,949; f90: 10,845; ansic: 7,967; sh: 4,226; perl: 4,064; fortran: 2,424; makefile: 1,501; objc: 238; lisp: 163; csh: 16; awk: 14; tcl: 6
file content (657 lines) | stat: -rw-r--r-- 24,217 bytes parent folder | download | duplicates (3)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/***************************************************************************
                                nvd_memory.h
                             -------------------
                               W. Michael Brown

  CUDA Driver Specific Memory Management and Vector/Matrix Containers

 __________________________________________________________________________
    This file is part of the Geryon Unified Coprocessor Library (UCL)
 __________________________________________________________________________

    begin                : Thu Jan 21 2010
    copyright            : (C) 2010 by W. Michael Brown
    email                : brownw@ornl.gov
 ***************************************************************************/

/* -----------------------------------------------------------------------
   Copyright (2010) Sandia Corporation.  Under the terms of Contract
   DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
   certain rights in this software.  This software is distributed under
   the Simplified BSD License.
   ----------------------------------------------------------------------- */

#ifndef NVD_MEMORY_H
#define NVD_MEMORY_H

#include <iostream>
#include <cassert>
#include <cstring>
#include "nvd_macros.h"
#include "ucl_types.h"

namespace ucl_cudadr {

// --------------------------------------------------------------------------
// - API Specific Types
// --------------------------------------------------------------------------
//typedef dim3 ucl_kernel_dim;

// --------------------------------------------------------------------------
// - API SPECIFIC DEVICE POINTERS
// --------------------------------------------------------------------------
typedef CUdeviceptr device_ptr;

// --------------------------------------------------------------------------
// - HOST MEMORY ALLOCATION ROUTINES
// --------------------------------------------------------------------------
template <class mat_type, class copy_type>
inline int _host_alloc(mat_type &mat, copy_type &cm, const size_t n,
                       const enum UCL_MEMOPT kind, const enum UCL_MEMOPT kind2){
  CUresult err=CUDA_SUCCESS;
  if (kind==UCL_NOT_PINNED)
    *(mat.host_ptr())=(typename mat_type::data_type*)malloc(n);
  else if (kind==UCL_WRITE_ONLY)
    err=cuMemHostAlloc((void **)mat.host_ptr(),n,CU_MEMHOSTALLOC_WRITECOMBINED);
  else
    err=cuMemAllocHost((void **)mat.host_ptr(),n);
  if (err!=CUDA_SUCCESS || *(mat.host_ptr())==nullptr)
    return UCL_MEMORY_ERROR;
  mat.cq()=cm.cq();
  return UCL_SUCCESS;
}

template <class mat_type>
inline int _host_alloc(mat_type &mat, UCL_Device &dev, const size_t n,
                       const enum UCL_MEMOPT kind, const enum UCL_MEMOPT kind2){
  CUresult err=CUDA_SUCCESS;
  if (kind==UCL_NOT_PINNED)
    *(mat.host_ptr())=(typename mat_type::data_type*)malloc(n);
  else if (kind==UCL_WRITE_ONLY)
    err=cuMemHostAlloc((void **)mat.host_ptr(),n,CU_MEMHOSTALLOC_WRITECOMBINED);
  else
    err=cuMemAllocHost((void **)mat.host_ptr(),n);
  if (err!=CUDA_SUCCESS || *(mat.host_ptr())==nullptr)
    return UCL_MEMORY_ERROR;
  mat.cq()=dev.cq();
  return UCL_SUCCESS;
}

template <class mat_type>
inline void _host_free(mat_type &mat) {
  if (mat.kind()==UCL_VIEW)
    return;
  else if (mat.kind()!=UCL_NOT_PINNED)
    CU_DESTRUCT_CALL(cuMemFreeHost(mat.begin()));
  else
    free(mat.begin());
}

template <class mat_type>
inline int _host_resize(mat_type &mat, const size_t n) {
  _host_free(mat);
  CUresult err=CUDA_SUCCESS;
  if (mat.kind()==UCL_NOT_PINNED)
    *(mat.host_ptr())=(typename mat_type::data_type*)malloc(n);
  else if (mat.kind()==UCL_WRITE_ONLY)
    err=cuMemHostAlloc((void **)mat.host_ptr(),n,CU_MEMHOSTALLOC_WRITECOMBINED);
  else
    err=cuMemAllocHost((void **)mat.host_ptr(),n);
  if (err!=CUDA_SUCCESS || *(mat.host_ptr())==nullptr)
    return UCL_MEMORY_ERROR;
  return UCL_SUCCESS;
}

// --------------------------------------------------------------------------
// - DEVICE MEMORY ALLOCATION ROUTINES
// --------------------------------------------------------------------------
template <class mat_type, class copy_type>
inline int _device_alloc(mat_type &mat, copy_type &cm, const size_t n,
                         const enum UCL_MEMOPT kind) {
  CUresult err=cuMemAlloc(&mat.cbegin(),n);
  if (err!=CUDA_SUCCESS)
    return UCL_MEMORY_ERROR;
  mat.cq()=cm.cq();
  return UCL_SUCCESS;
}

template <class mat_type>
inline int _device_alloc(mat_type &mat, UCL_Device &dev, const size_t n,
                         const enum UCL_MEMOPT kind) {
  CUresult err=cuMemAlloc(&mat.cbegin(),n);
  if (err!=CUDA_SUCCESS)
    return UCL_MEMORY_ERROR;
  mat.cq()=dev.cq();
  return UCL_SUCCESS;
}

template <class mat_type, class copy_type>
inline int _device_alloc(mat_type &mat, copy_type &cm, const size_t rows,
                         const size_t cols, size_t &pitch,
                         const enum UCL_MEMOPT kind) {
  CUresult err;
  CUDA_INT_TYPE upitch;
  err=cuMemAllocPitch(&mat.cbegin(),&upitch,
                      cols*sizeof(typename mat_type::data_type),rows,16);
  pitch=static_cast<size_t>(upitch);
  if (err!=CUDA_SUCCESS)
    return UCL_MEMORY_ERROR;
  mat.cq()=cm.cq();
  return UCL_SUCCESS;
}

template <class mat_type, class copy_type>
inline int _device_alloc(mat_type &mat, UCL_Device &d, const size_t rows,
                         const size_t cols, size_t &pitch,
                         const enum UCL_MEMOPT kind) {
  CUresult err;
  unsigned upitch;
  err=cuMemAllocPitch(&mat.cbegin(),&upitch,
                      cols*sizeof(typename mat_type::data_type),rows,16);
  pitch=static_cast<size_t>(upitch);
  if (err!=CUDA_SUCCESS)
    return UCL_MEMORY_ERROR;
  mat.cq()=d.cq();
  return UCL_SUCCESS;
}

template <class mat_type>
inline void _device_free(mat_type &mat) {
  if (mat.kind()!=UCL_VIEW)
    CU_DESTRUCT_CALL(cuMemFree(mat.cbegin()));
}

template <class mat_type>
inline int _device_resize(mat_type &mat, const size_t n) {
  _device_free(mat);
  CUresult err=cuMemAlloc(&mat.cbegin(),n);
  if (err!=CUDA_SUCCESS)
    return UCL_MEMORY_ERROR;
  return UCL_SUCCESS;
}

template <class mat_type>
inline int _device_resize(mat_type &mat, const size_t rows,
                          const size_t cols, size_t &pitch) {
  _device_free(mat);
  CUresult err;
  CUDA_INT_TYPE upitch;
  err=cuMemAllocPitch(&mat.cbegin(),&upitch,
                      cols*sizeof(typename mat_type::data_type),rows,16);
  pitch=static_cast<size_t>(upitch);
  if (err!=CUDA_SUCCESS)
    return UCL_MEMORY_ERROR;
  return UCL_SUCCESS;
}

inline void _device_view(CUdeviceptr *ptr, CUdeviceptr &in) {
  *ptr=in;
}

template <class numtyp>
inline void _device_view(CUdeviceptr *ptr, numtyp *in) {
  *ptr=0;
}

inline void _device_view(CUdeviceptr *ptr, CUdeviceptr &in,
                         const size_t offset, const size_t numsize) {
  *ptr=in+offset*numsize;
}

template <class numtyp>
inline void _device_view(CUdeviceptr *ptr, numtyp *in,
                         const size_t offset, const size_t numsize) {
  *ptr=0;
}

// --------------------------------------------------------------------------
// - DEVICE IMAGE ALLOCATION ROUTINES
// --------------------------------------------------------------------------
template <class mat_type, class copy_type>
inline void _device_image_alloc(mat_type &mat, copy_type &cm, const size_t rows,
                                const size_t cols) {
  assert(0==1);
}

template <class mat_type, class copy_type>
inline void _device_image_alloc(mat_type &mat, UCL_Device &d, const size_t rows,
                                const size_t cols) {
  assert(0==1);
}

template <class mat_type>
inline void _device_image_free(mat_type &mat) {
  assert(0==1);
}

// --------------------------------------------------------------------------
// - ZERO ROUTINES
// --------------------------------------------------------------------------
inline void _host_zero(void *ptr, const size_t n) {
  memset(ptr,0,n);
}

template <class mat_type>
inline void _device_zero(mat_type &mat, const size_t n, command_queue &cq) {
  if (n%32==0)
    CU_SAFE_CALL(cuMemsetD32Async(mat.cbegin(),0,n/4,cq));
  else if (n%16==0)
    CU_SAFE_CALL(cuMemsetD16Async(mat.cbegin(),0,n/2,cq));
  else
    CU_SAFE_CALL(cuMemsetD8Async(mat.cbegin(),0,n,cq));
}

// --------------------------------------------------------------------------
// - HELPER FUNCTIONS FOR MEMCPY ROUTINES
// --------------------------------------------------------------------------

inline void _nvd_set_2D_loc(CUDA_MEMCPY2D &ins, const size_t dpitch,
                            const size_t spitch, const size_t cols,
                            const size_t rows) {
  ins.srcXInBytes=0;
  ins.srcY=0;
  ins.srcPitch=spitch;
  ins.dstXInBytes=0;
  ins.dstY=0;
  ins.dstPitch=dpitch;
  ins.WidthInBytes=cols;
  ins.Height=rows;
}

template <int mem> struct _nvd_set_2D_mem;
template <> struct _nvd_set_2D_mem<1>
  { static CUmemorytype a() { return CU_MEMORYTYPE_HOST; } };
template <> struct _nvd_set_2D_mem<2>
  { static CUmemorytype a() { return CU_MEMORYTYPE_ARRAY; } };
template <int mem> struct _nvd_set_2D_mem
  { static CUmemorytype a() { return CU_MEMORYTYPE_DEVICE; } };


// --------------------------------------------------------------------------
// - MEMCPY ROUTINES
// --------------------------------------------------------------------------

template<int mem1, int mem2> struct _ucl_memcpy;

// Both are images
template<> struct _ucl_memcpy<2,2> {
  template <class p1, class p2>
  static inline void mc(p1 &dst, const p2 &src, const size_t n) {
    assert(0==1);
  }
  template <class p1, class p2>
  static inline void mc(p1 &dst, const p2 &src, const size_t n,
                        CUstream &cq) {
    assert(0==1);
  }
  template <class p1, class p2>
      static inline void mc(p1 &dst, const size_t dpitch, const p2 &src,
                            const size_t spitch, const size_t cols,
                            const size_t rows) {
    CUDA_MEMCPY2D ins;
    _nvd_set_2D_loc(ins,dpitch,spitch,cols,rows);
    ins.dstMemoryType=_nvd_set_2D_mem<p1::MEM_TYPE>::a();
    ins.srcMemoryType=_nvd_set_2D_mem<p2::MEM_TYPE>::a();
    ins.dstArray=dst.cbegin();
    ins.srcArray=src.cbegin();
    CU_SAFE_CALL(cuMemcpy2D(&ins));
  }
  template <class p1, class p2>
      static inline void mc(p1 &dst, const size_t dpitch, const p2 &src,
                            const size_t spitch, const size_t cols,
                            const size_t rows, CUstream &cq) {
    CUDA_MEMCPY2D ins;
    _nvd_set_2D_loc(ins,dpitch,spitch,cols,rows);
    ins.dstMemoryType=_nvd_set_2D_mem<p1::MEM_TYPE>::a();
    ins.srcMemoryType=_nvd_set_2D_mem<p2::MEM_TYPE>::a();
    ins.dstArray=dst.cbegin();
    ins.srcArray=src.cbegin();
    CU_SAFE_CALL(cuMemcpy2DAsync(&ins,cq));
  }
};

// Destination is texture, source on device
template<> struct _ucl_memcpy<2,0> {
  template <class p1, class p2>
  static inline void mc(p1 &dst, const p2 &src, const size_t n) {
    assert(0==1);
  }
  template <class p1, class p2>
  static inline void mc(p1 &dst, const p2 &src, const size_t n,
                        CUstream &cq) {
    assert(0==1);
  }
  template <class p1, class p2>
      static inline void mc(p1 &dst, const size_t dpitch, const p2 &src,
                            const size_t spitch, const size_t cols,
                            const size_t rows) {
    CUDA_MEMCPY2D ins;
    _nvd_set_2D_loc(ins,dpitch,spitch,cols,rows);
    ins.dstMemoryType=_nvd_set_2D_mem<p1::MEM_TYPE>::a();
    ins.srcMemoryType=_nvd_set_2D_mem<p2::MEM_TYPE>::a();
    ins.dstArray=dst.cbegin();
    ins.srcDevice=src.cbegin();
    CU_SAFE_CALL(cuMemcpy2D(&ins));
  }
  template <class p1, class p2>
      static inline void mc(p1 &dst, const size_t dpitch, const p2 &src,
                            const size_t spitch, const size_t cols,
                            const size_t rows, CUstream &cq) {
    CUDA_MEMCPY2D ins;
    _nvd_set_2D_loc(ins,dpitch,spitch,cols,rows);
    ins.dstMemoryType=_nvd_set_2D_mem<p1::MEM_TYPE>::a();
    ins.srcMemoryType=_nvd_set_2D_mem<p2::MEM_TYPE>::a();
    ins.dstArray=dst.cbegin();
    ins.srcDevice=src.cbegin();
    CU_SAFE_CALL(cuMemcpy2DAsync(&ins,cq));
  }
};

// Destination is texture, source on host
template<> struct _ucl_memcpy<2,1> {
  template <class p1, class p2>
  static inline void mc(p1 &dst, const p2 &src, const size_t n) {
    assert(0==1);
  }
  template <class p1, class p2>
  static inline void mc(p1 &dst, const p2 &src, const size_t n,
                        CUstream &cq) {
    assert(0==1);
  }
  template <class p1, class p2>
      static inline void mc(p1 &dst, const size_t dpitch, const p2 &src,
                            const size_t spitch, const size_t cols,
                            const size_t rows) {
    CUDA_MEMCPY2D ins;
    _nvd_set_2D_loc(ins,dpitch,spitch,cols,rows);
    ins.dstMemoryType=_nvd_set_2D_mem<p1::MEM_TYPE>::a();
    ins.srcMemoryType=_nvd_set_2D_mem<p2::MEM_TYPE>::a();
    ins.dstArray=dst.cbegin();
    ins.srcHost=src.begin();
    CU_SAFE_CALL(cuMemcpy2D(&ins));
  }
  template <class p1, class p2>
      static inline void mc(p1 &dst, const size_t dpitch, const p2 &src,
                            const size_t spitch, const size_t cols,
                            const size_t rows, CUstream &cq) {
    CUDA_MEMCPY2D ins;
    _nvd_set_2D_loc(ins,dpitch,spitch,cols,rows);
    ins.dstMemoryType=_nvd_set_2D_mem<p1::MEM_TYPE>::a();
    ins.srcMemoryType=_nvd_set_2D_mem<p2::MEM_TYPE>::a();
    ins.dstArray=dst.cbegin();
    ins.srcHost=src.begin();
    CU_SAFE_CALL(cuMemcpy2DAsync(&ins,cq));
  }
};

// Source is texture, dest on device
template<> struct _ucl_memcpy<0,2> {
  template <class p1, class p2>
  static inline void mc(p1 &dst, const p2 &src, const size_t n) {
    assert(0==1);
  }
  template <class p1, class p2>
  static inline void mc(p1 &dst, const p2 &src, const size_t n,
                        CUstream &cq) {
    assert(0==1);
  }
  template <class p1, class p2>
      static inline void mc(p1 &dst, const size_t dpitch, const p2 &src,
                            const size_t spitch, const size_t cols,
                            const size_t rows) {
    CUDA_MEMCPY2D ins;
    _nvd_set_2D_loc(ins,dpitch,spitch,cols,rows);
    ins.dstMemoryType=_nvd_set_2D_mem<p1::MEM_TYPE>::a();
    ins.srcMemoryType=_nvd_set_2D_mem<p2::MEM_TYPE>::a();
    ins.dstDevice=dst.cbegin();
    ins.srcArray=src.cbegin();
    CU_SAFE_CALL(cuMemcpy2D(&ins));
  }
  template <class p1, class p2>
      static inline void mc(p1 &dst, const size_t dpitch, const p2 &src,
                            const size_t spitch, const size_t cols,
                            const size_t rows, CUstream &cq) {
    CUDA_MEMCPY2D ins;
    _nvd_set_2D_loc(ins,dpitch,spitch,cols,rows);
    ins.dstMemoryType=_nvd_set_2D_mem<p1::MEM_TYPE>::a();
    ins.srcMemoryType=_nvd_set_2D_mem<p2::MEM_TYPE>::a();
    ins.dstDevice=dst.cbegin();
    ins.srcArray=src.cbegin();
    CU_SAFE_CALL(cuMemcpy2DAsync(&ins,cq));
  }
};

// Source is texture, dest on host
template<> struct _ucl_memcpy<1,2> {
  template <class p1, class p2>
  static inline void mc(p1 &dst, const p2 &src, const size_t n) {
    assert(0==1);
  }
  template <class p1, class p2>
  static inline void mc(p1 &dst, const p2 &src, const size_t n,
                        CUstream &cq) {
    assert(0==1);
  }
  template <class p1, class p2>
      static inline void mc(p1 &dst, const size_t dpitch, const p2 &src,
                            const size_t spitch, const size_t cols,
                            const size_t rows) {
    CUDA_MEMCPY2D ins;
    _nvd_set_2D_loc(ins,dpitch,spitch,cols,rows);
    ins.dstMemoryType=_nvd_set_2D_mem<p1::MEM_TYPE>::a();
    ins.srcMemoryType=_nvd_set_2D_mem<p2::MEM_TYPE>::a();
    ins.dstHost=dst.begin();
    ins.srcArray=src.cbegin();
    CU_SAFE_CALL(cuMemcpy2D(&ins));
  }
  template <class p1, class p2>
      static inline void mc(p1 &dst, const size_t dpitch, const p2 &src,
                            const size_t spitch, const size_t cols,
                            const size_t rows, CUstream &cq) {
    CUDA_MEMCPY2D ins;
    _nvd_set_2D_loc(ins,dpitch,spitch,cols,rows);
    ins.dstMemoryType=_nvd_set_2D_mem<p1::MEM_TYPE>::a();
    ins.srcMemoryType=_nvd_set_2D_mem<p2::MEM_TYPE>::a();
    ins.dstHost=dst.begin();
    ins.srcArray=src.cbegin();
    CU_SAFE_CALL(cuMemcpy2DAsync(&ins,cq));
  }
};

// Neither are textures, destination on host
template <> struct _ucl_memcpy<1,0> {
  template <class p1, class p2>
  static inline void mc(p1 &dst, const p2 &src, const size_t n) {
    CU_SAFE_CALL(cuMemcpyDtoH(dst.begin(),src.cbegin(),n));
  }
  template <class p1, class p2>
  static inline void mc(p1 &dst, const p2 &src, const size_t n,
                        CUstream &cq) {
    CU_SAFE_CALL(cuMemcpyDtoHAsync(dst.begin(),src.cbegin(),n,cq));
  }
  template <class p1, class p2>
      static inline void mc(p1 &dst, const size_t dpitch, const p2 &src,
                            const size_t spitch, const size_t cols,
                            const size_t rows) {
    CUDA_MEMCPY2D ins;
    _nvd_set_2D_loc(ins,dpitch,spitch,cols,rows);
    ins.dstMemoryType=_nvd_set_2D_mem<p1::MEM_TYPE>::a();
    ins.srcMemoryType=_nvd_set_2D_mem<p2::MEM_TYPE>::a();
    ins.dstHost=dst.begin();
    ins.srcDevice=src.cbegin();
    CU_SAFE_CALL(cuMemcpy2D(&ins));
  }
  template <class p1, class p2>
      static inline void mc(p1 &dst, const size_t dpitch, const p2 &src,
                            const size_t spitch, const size_t cols,
                            const size_t rows, CUstream &cq) {
    CUDA_MEMCPY2D ins;
    _nvd_set_2D_loc(ins,dpitch,spitch,cols,rows);
    ins.dstMemoryType=_nvd_set_2D_mem<p1::MEM_TYPE>::a();
    ins.srcMemoryType=_nvd_set_2D_mem<p2::MEM_TYPE>::a();
    ins.dstHost=dst.begin();
    ins.srcDevice=src.cbegin();
    CU_SAFE_CALL(cuMemcpy2DAsync(&ins,cq));
  }
};

// Neither are textures, source on host
template <> struct _ucl_memcpy<0,1> {
  template <class p1, class p2>
  static inline void mc(p1 &dst, const p2 &src, const size_t n) {
    CU_SAFE_CALL(cuMemcpyHtoD(dst.cbegin(),src.begin(),n));
  }
  template <class p1, class p2>
  static inline void mc(p1 &dst, const p2 &src, const size_t n,
                        CUstream &cq) {
    CU_SAFE_CALL(cuMemcpyHtoDAsync(dst.cbegin(),src.begin(),n,cq));
  }
  template <class p1, class p2>
      static inline void mc(p1 &dst, const size_t dpitch, const p2 &src,
                            const size_t spitch, const size_t cols,
                            const size_t rows) {
    CUDA_MEMCPY2D ins;
    _nvd_set_2D_loc(ins,dpitch,spitch,cols,rows);
    ins.dstMemoryType=_nvd_set_2D_mem<p1::MEM_TYPE>::a();
    ins.srcMemoryType=_nvd_set_2D_mem<p2::MEM_TYPE>::a();
    ins.dstDevice=dst.cbegin();
    ins.srcHost=src.begin();
    CU_SAFE_CALL(cuMemcpy2D(&ins));
  }
  template <class p1, class p2>
      static inline void mc(p1 &dst, const size_t dpitch, const p2 &src,
                            const size_t spitch, const size_t cols,
                            const size_t rows, CUstream &cq) {
    CUDA_MEMCPY2D ins;
    _nvd_set_2D_loc(ins,dpitch,spitch,cols,rows);
    ins.dstMemoryType=_nvd_set_2D_mem<p1::MEM_TYPE>::a();
    ins.srcMemoryType=_nvd_set_2D_mem<p2::MEM_TYPE>::a();
    ins.dstDevice=dst.cbegin();
    ins.srcHost=src.begin();
    CU_SAFE_CALL(cuMemcpy2DAsync(&ins,cq));
  }
};

// Neither are textures, both on host
template <> struct _ucl_memcpy<1,1> {
  template <class p1, class p2>
  static inline void mc(p1 &dst, const p2 &src, const size_t n)
    { memcpy(dst.begin(),src.begin(),n); }
  template <class p1, class p2>
  static inline void mc(p1 &dst, const p2 &src, const size_t n,
                        CUstream &cq)
    { memcpy(dst.begin(),src.begin(),n); }
  template <class p1, class p2>
      static inline void mc(p1 &dst, const size_t dpitch, const p2 &src,
                            const size_t spitch, const size_t cols,
                            const size_t rows) {
    CUDA_MEMCPY2D ins;
    _nvd_set_2D_loc(ins,dpitch,spitch,cols,rows);
    ins.dstMemoryType=_nvd_set_2D_mem<p1::MEM_TYPE>::a();
    ins.srcMemoryType=_nvd_set_2D_mem<p2::MEM_TYPE>::a();
    ins.dstHost=dst.begin();
    ins.srcHost=src.begin();
    CU_SAFE_CALL(cuMemcpy2D(&ins));
  }
  template <class p1, class p2>
      static inline void mc(p1 &dst, const size_t dpitch, const p2 &src,
                            const size_t spitch, const size_t cols,
                            const size_t rows, CUstream &cq) {
    CUDA_MEMCPY2D ins;
    _nvd_set_2D_loc(ins,dpitch,spitch,cols,rows);
    ins.dstMemoryType=_nvd_set_2D_mem<p1::MEM_TYPE>::a();
    ins.srcMemoryType=_nvd_set_2D_mem<p2::MEM_TYPE>::a();
    ins.dstHost=dst.begin();
    ins.srcHost=src.begin();
    CU_SAFE_CALL(cuMemcpy2DAsync(&ins,cq));
  }
};

// Neither are textures, both on device
template <int mem1, int mem2> struct _ucl_memcpy {
  template <class p1, class p2>
  static inline void mc(p1 &dst, const p2 &src, const size_t n) {
    CU_SAFE_CALL(cuMemcpyDtoD(dst.cbegin(),src.cbegin(),n));
  }
  template <class p1, class p2>
  static inline void mc(p1 &dst, const p2 &src, const size_t n,
                        CUstream &cq) {
    CU_SAFE_CALL(cuMemcpyDtoDAsync(dst.cbegin(),src.cbegin(),n,cq));
  }
  template <class p1, class p2>
      static inline void mc(p1 &dst, const size_t dpitch, const p2 &src,
                            const size_t spitch, const size_t cols,
                            const size_t rows) {
    if (p1::PADDED==0 || p2::PADDED==0) {
      size_t src_offset=0, dst_offset=0;
      for (size_t i=0; i<rows; i++) {
        CU_SAFE_CALL(cuMemcpyDtoD(dst.cbegin()+dst_offset,
                                  src.cbegin()+src_offset,cols));
        src_offset+=spitch;
        dst_offset+=dpitch;
      }
    } else {
      CUDA_MEMCPY2D ins;
      _nvd_set_2D_loc(ins,dpitch,spitch,cols,rows);
      ins.dstMemoryType=_nvd_set_2D_mem<p1::MEM_TYPE>::a();
      ins.srcMemoryType=_nvd_set_2D_mem<p2::MEM_TYPE>::a();
      ins.dstDevice=dst.cbegin();
      ins.srcDevice=src.cbegin();
      CU_SAFE_CALL(cuMemcpy2D(&ins));
    }
  }
  template <class p1, class p2>
      static inline void mc(p1 &dst, const size_t dpitch, const p2 &src,
                            const size_t spitch, const size_t cols,
                            const size_t rows, CUstream &cq) {
    if (p1::PADDED==0 || p2::PADDED==0) {
      size_t src_offset=0, dst_offset=0;
      for (size_t i=0; i<rows; i++) {
        CU_SAFE_CALL(cuMemcpyDtoDAsync(dst.cbegin()+dst_offset,
                                       src.cbegin()+src_offset,cols,cq));
        src_offset+=spitch;
        dst_offset+=dpitch;
      }
    } else {
      CUDA_MEMCPY2D ins;
      _nvd_set_2D_loc(ins,dpitch,spitch,cols,rows);
      ins.dstMemoryType=_nvd_set_2D_mem<p1::MEM_TYPE>::a();
      ins.srcMemoryType=_nvd_set_2D_mem<p2::MEM_TYPE>::a();
      ins.dstDevice=dst.cbegin();
      ins.srcDevice=src.cbegin();
      CU_SAFE_CALL(cuMemcpy2DAsync(&ins,cq));
    }
  }
};

template<class mat1, class mat2>
inline void ucl_mv_cpy(mat1 &dst, const mat2 &src, const size_t n) {
  _ucl_memcpy<mat1::MEM_TYPE,mat2::MEM_TYPE>::mc(dst,src,n);
}

template<class mat1, class mat2>
inline void ucl_mv_cpy(mat1 &dst, const mat2 &src, const size_t n,
                       CUstream &cq) {
  _ucl_memcpy<mat1::MEM_TYPE,mat2::MEM_TYPE>::mc(dst,src,n,cq);
}

template<class mat1, class mat2>
inline void ucl_mv_cpy(mat1 &dst, const size_t dpitch, const mat2 &src,
                       const size_t spitch, const size_t cols,
                       const size_t rows) {
  _ucl_memcpy<mat1::MEM_TYPE,mat2::MEM_TYPE>::mc(dst,dpitch,src,spitch,cols,
                                                 rows);
}

template<class mat1, class mat2>
inline void ucl_mv_cpy(mat1 &dst, const size_t dpitch, const mat2 &src,
                       const size_t spitch, const size_t cols,
                       const size_t rows,CUstream &cq) {
  _ucl_memcpy<mat1::MEM_TYPE,mat2::MEM_TYPE>::mc(dst,dpitch,src,spitch,cols,
                                                 rows,cq);
}

} // namespace ucl_cudart

#endif