2016-01-26 20:14:33 -06:00
|
|
|
/*
|
|
|
|
|
Copyright (c) 2015-2016 Advanced Micro Devices, Inc. All rights reserved.
|
|
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
*/
|
2016-09-27 17:24:33 +05:30
|
|
|
|
|
|
|
|
/* HIT_START
|
2017-02-27 13:17:51 +05:30
|
|
|
* BUILD: %t %s ../../test_common.cpp NVCC_OPTIONS -std=c++11
|
2016-09-27 17:24:33 +05:30
|
|
|
* RUN_NAMED: %t hipMemcpy-modes --tests 0x1
|
|
|
|
|
* RUN_NAMED: %t hipMemcpy-size --tests 0x6
|
2017-04-24 21:22:56 -05:00
|
|
|
* RUN_NAMED: %t hipMemcpy-dev-offsets --tests 0x10
|
|
|
|
|
* RUN_NAMED: %t hipMemcpy-host-offsets --tests 0x20
|
2016-09-27 17:24:33 +05:30
|
|
|
* RUN_NAMED: %t hipMemcpy-multithreaded --tests 0x8
|
|
|
|
|
* HIT_END
|
|
|
|
|
*/
|
|
|
|
|
|
2016-10-04 22:20:50 +05:30
|
|
|
#include "hip/hip_runtime.h"
|
|
|
|
|
#include "hip/hip_runtime.h"
|
2016-01-26 20:14:33 -06:00
|
|
|
#include "test_common.h"
|
|
|
|
|
|
2016-02-12 18:23:55 -06:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
void printSep() {
|
|
|
|
|
printf(
|
|
|
|
|
"======================================================================================\n");
|
2016-02-13 01:15:23 -06:00
|
|
|
}
|
2016-01-26 20:14:33 -06:00
|
|
|
|
2017-04-24 11:53:31 -05:00
|
|
|
//-------
|
2018-03-12 11:29:03 +05:30
|
|
|
template <typename T>
|
|
|
|
|
class DeviceMemory {
|
|
|
|
|
public:
|
2017-04-24 11:53:31 -05:00
|
|
|
DeviceMemory(size_t numElements);
|
|
|
|
|
~DeviceMemory();
|
2017-04-24 12:51:17 -05:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
T* A_d() const { return _A_d + _offset; };
|
|
|
|
|
T* B_d() const { return _B_d + _offset; };
|
|
|
|
|
T* C_d() const { return _C_d + _offset; };
|
|
|
|
|
T* C_dd() const { return _C_dd + _offset; };
|
2017-04-24 12:51:17 -05:00
|
|
|
|
|
|
|
|
size_t maxNumElements() const { return _maxNumElements; };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void offset(int offset) { _offset = offset; };
|
|
|
|
|
int offset() const { return _offset; };
|
This switches HIP from its currently convoluted macro + pfe based dispatch mechanism to a more natural one partially based on the existing module API. The basic idea is that HCC will always correctly emit __global__ functions: as empty-bodied stubs, on host, and as kernels, on device. It then becomes trivial to obtain the mangled name on host, at dispatch, from the function's address, and then to use the mangled name to retrieve the kernel. This should address all problems stemming from serialisation, dubious mismatches due to the manufactured functor, macro-isms et al. It also immediately enables support for generalised globals as a consequence of that being available in the module API. Finally, it will make debug much easier, since the actual names of the __global__ functions will automatically be used in traces etc. One detail is that due to how dispatch works now (hipLaunchKernel and hipLaunchKernelGGL are themselves variadic function templates which deduce the function type of the callee), in certain cases it may be necesssary to insert explicit casts to ensure that the variadic argument list selects a viable overload - this can be observed in some unit tests. Eventually we may be able to remove this limitation, but for now it does not appear terribly onerous. The code is not extremely HIPpie, nor is it fully optimised, but rather is intended as a starting point for the HIP team to make its own.
2017-11-01 15:09:59 +00:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
private:
|
|
|
|
|
T* _A_d;
|
|
|
|
|
T* _B_d;
|
|
|
|
|
T* _C_d;
|
|
|
|
|
T* _C_dd;
|
2017-04-24 12:51:17 -05:00
|
|
|
|
2017-04-24 11:53:31 -05:00
|
|
|
|
|
|
|
|
size_t _maxNumElements;
|
2017-04-24 12:51:17 -05:00
|
|
|
int _offset;
|
2017-04-24 11:53:31 -05:00
|
|
|
};
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
template <typename T>
|
|
|
|
|
DeviceMemory<T>::DeviceMemory(size_t numElements) : _maxNumElements(numElements), _offset(0) {
|
|
|
|
|
T** np = nullptr;
|
|
|
|
|
HipTest::initArrays(&_A_d, &_B_d, &_C_d, np, np, np, numElements, 0);
|
2017-04-24 11:53:31 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
size_t sizeElements = numElements * sizeof(T);
|
|
|
|
|
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
HIPCHECK(hipMalloc(&_C_dd, sizeElements));
|
2017-04-24 11:53:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
template <typename T>
|
|
|
|
|
DeviceMemory<T>::~DeviceMemory() {
|
|
|
|
|
T* np = nullptr;
|
|
|
|
|
HipTest::freeArrays(_A_d, _B_d, _C_d, np, np, np, 0);
|
2017-04-24 11:53:31 -05:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
HIPCHECK(hipFree(_C_dd));
|
This switches HIP from its currently convoluted macro + pfe based dispatch mechanism to a more natural one partially based on the existing module API. The basic idea is that HCC will always correctly emit __global__ functions: as empty-bodied stubs, on host, and as kernels, on device. It then becomes trivial to obtain the mangled name on host, at dispatch, from the function's address, and then to use the mangled name to retrieve the kernel. This should address all problems stemming from serialisation, dubious mismatches due to the manufactured functor, macro-isms et al. It also immediately enables support for generalised globals as a consequence of that being available in the module API. Finally, it will make debug much easier, since the actual names of the __global__ functions will automatically be used in traces etc. One detail is that due to how dispatch works now (hipLaunchKernel and hipLaunchKernelGGL are themselves variadic function templates which deduce the function type of the callee), in certain cases it may be necesssary to insert explicit casts to ensure that the variadic argument list selects a viable overload - this can be observed in some unit tests. Eventually we may be able to remove this limitation, but for now it does not appear terribly onerous. The code is not extremely HIPpie, nor is it fully optimised, but rather is intended as a starting point for the HIP team to make its own.
2017-11-01 15:09:59 +00:00
|
|
|
|
2017-04-24 12:51:17 -05:00
|
|
|
_C_dd = NULL;
|
2017-04-24 11:53:31 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-------
|
2018-03-12 11:29:03 +05:30
|
|
|
template <typename T>
|
|
|
|
|
class HostMemory {
|
|
|
|
|
public:
|
2017-04-24 11:53:31 -05:00
|
|
|
HostMemory(size_t numElements, bool usePinnedHost);
|
2018-03-12 11:29:03 +05:30
|
|
|
void reset(size_t numElements, bool full = false);
|
2017-04-24 11:53:31 -05:00
|
|
|
~HostMemory();
|
2017-04-24 12:51:17 -05:00
|
|
|
|
2017-04-24 21:22:56 -05:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
T* A_h() const { return _A_h + _offset; };
|
|
|
|
|
T* B_h() const { return _B_h + _offset; };
|
|
|
|
|
T* C_h() const { return _C_h + _offset; };
|
2017-04-24 21:22:56 -05:00
|
|
|
|
|
|
|
|
|
2017-04-24 12:51:17 -05:00
|
|
|
size_t maxNumElements() const { return _maxNumElements; };
|
2017-04-24 21:22:56 -05:00
|
|
|
|
|
|
|
|
void offset(int offset) { _offset = offset; };
|
|
|
|
|
int offset() const { return _offset; };
|
2017-04-24 11:53:31 -05:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
public:
|
2017-04-24 11:53:31 -05:00
|
|
|
// Host arrays, secondary copy
|
2018-03-12 11:29:03 +05:30
|
|
|
T* A_hh;
|
|
|
|
|
T* B_hh;
|
|
|
|
|
|
|
|
|
|
bool _usePinnedHost;
|
2017-04-24 11:53:31 -05:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
private:
|
2017-04-24 21:22:56 -05:00
|
|
|
size_t _maxNumElements;
|
|
|
|
|
|
|
|
|
|
int _offset;
|
|
|
|
|
|
|
|
|
|
// Host arrays
|
2018-03-12 11:29:03 +05:30
|
|
|
T* _A_h;
|
|
|
|
|
T* _B_h;
|
|
|
|
|
T* _C_h;
|
2017-04-24 11:53:31 -05:00
|
|
|
};
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
template <typename T>
|
2017-04-24 11:53:31 -05:00
|
|
|
HostMemory<T>::HostMemory(size_t numElements, bool usePinnedHost)
|
2018-03-12 11:29:03 +05:30
|
|
|
: _maxNumElements(numElements), _usePinnedHost(usePinnedHost), _offset(0) {
|
|
|
|
|
T** np = nullptr;
|
|
|
|
|
HipTest::initArrays(np, np, np, &_A_h, &_B_h, &_C_h, numElements, usePinnedHost);
|
2017-04-24 11:53:31 -05:00
|
|
|
|
|
|
|
|
A_hh = NULL;
|
|
|
|
|
B_hh = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
size_t sizeElements = numElements * sizeof(T);
|
|
|
|
|
|
|
|
|
|
if (usePinnedHost) {
|
2018-03-12 11:29:03 +05:30
|
|
|
HIPCHECK(hipHostMalloc((void**)&A_hh, sizeElements, hipHostMallocDefault));
|
|
|
|
|
HIPCHECK(hipHostMalloc((void**)&B_hh, sizeElements, hipHostMallocDefault));
|
2017-04-24 11:53:31 -05:00
|
|
|
} else {
|
|
|
|
|
A_hh = (T*)malloc(sizeElements);
|
|
|
|
|
B_hh = (T*)malloc(sizeElements);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
template <typename T>
|
|
|
|
|
void HostMemory<T>::reset(size_t numElements, bool full) {
|
2017-04-24 11:53:31 -05:00
|
|
|
// Initialize the host data:
|
2018-03-12 11:29:03 +05:30
|
|
|
for (size_t i = 0; i < numElements; i++) {
|
This switches HIP from its currently convoluted macro + pfe based dispatch mechanism to a more natural one partially based on the existing module API. The basic idea is that HCC will always correctly emit __global__ functions: as empty-bodied stubs, on host, and as kernels, on device. It then becomes trivial to obtain the mangled name on host, at dispatch, from the function's address, and then to use the mangled name to retrieve the kernel. This should address all problems stemming from serialisation, dubious mismatches due to the manufactured functor, macro-isms et al. It also immediately enables support for generalised globals as a consequence of that being available in the module API. Finally, it will make debug much easier, since the actual names of the __global__ functions will automatically be used in traces etc. One detail is that due to how dispatch works now (hipLaunchKernel and hipLaunchKernelGGL are themselves variadic function templates which deduce the function type of the callee), in certain cases it may be necesssary to insert explicit casts to ensure that the variadic argument list selects a viable overload - this can be observed in some unit tests. Eventually we may be able to remove this limitation, but for now it does not appear terribly onerous. The code is not extremely HIPpie, nor is it fully optimised, but rather is intended as a starting point for the HIP team to make its own.
2017-11-01 15:09:59 +00:00
|
|
|
(A_hh)[i] = 1097.0 + i;
|
2018-03-12 11:29:03 +05:30
|
|
|
(B_hh)[i] = 1492.0 + i; // Phi
|
2017-04-24 11:53:31 -05:00
|
|
|
|
|
|
|
|
if (full) {
|
2018-03-12 11:29:03 +05:30
|
|
|
(_A_h)[i] = 3.146f + i; // Pi
|
|
|
|
|
(_B_h)[i] = 1.618f + i; // Phi
|
2017-04-24 11:53:31 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
template <typename T>
|
|
|
|
|
HostMemory<T>::~HostMemory() {
|
|
|
|
|
HipTest::freeArraysForHost(_A_h, _B_h, _C_h, _usePinnedHost);
|
2017-04-24 11:53:31 -05:00
|
|
|
|
|
|
|
|
if (_usePinnedHost) {
|
2018-03-12 11:29:03 +05:30
|
|
|
HIPCHECK(hipHostFree(A_hh));
|
|
|
|
|
HIPCHECK(hipHostFree(B_hh));
|
2017-04-24 11:53:31 -05:00
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
free(A_hh);
|
|
|
|
|
free(B_hh);
|
|
|
|
|
}
|
|
|
|
|
};
|
2016-02-12 17:39:44 -06:00
|
|
|
|
|
|
|
|
|
2016-02-16 01:58:24 -06:00
|
|
|
//---
|
|
|
|
|
// Test many different kinds of memory copies.
|
2018-03-12 11:29:03 +05:30
|
|
|
// The subroutine allocates memory , copies to device, runs a vector add kernel, copies back, and
|
|
|
|
|
// checks the result.
|
2016-02-16 01:58:24 -06:00
|
|
|
//
|
|
|
|
|
// IN: numElements controls the number of elements used for allocations.
|
2018-03-12 11:29:03 +05:30
|
|
|
// IN: usePinnedHost : If true, allocate host with hipHostMalloc and is pinned ; else allocate host
|
|
|
|
|
// memory with malloc. IN: useHostToHost : If true, add an extra host-to-host copy. IN:
|
|
|
|
|
// useDeviceToDevice : If true, add an extra deviceto-device copy after result is produced. IN:
|
|
|
|
|
// useMemkindDefault : If true, use memkinddefault (runtime figures out direction). if false, use
|
|
|
|
|
// explicit memcpy direction.
|
2016-02-16 01:58:24 -06:00
|
|
|
//
|
2016-02-12 17:39:44 -06:00
|
|
|
template <typename T>
|
2018-03-12 11:29:03 +05:30
|
|
|
void memcpytest2(DeviceMemory<T>* dmem, HostMemory<T>* hmem, size_t numElements, bool useHostToHost,
|
|
|
|
|
bool useDeviceToDevice, bool useMemkindDefault) {
|
2016-02-13 01:15:23 -06:00
|
|
|
size_t sizeElements = numElements * sizeof(T);
|
2018-03-12 11:29:03 +05:30
|
|
|
printf(
|
|
|
|
|
"test: %s<%s> size=%lu (%6.2fMB) usePinnedHost:%d, useHostToHost:%d, useDeviceToDevice:%d, "
|
|
|
|
|
"useMemkindDefault:%d, offsets:dev:%+d host:+%d\n",
|
|
|
|
|
__func__, TYPENAME(T), sizeElements, sizeElements / 1024.0 / 1024.0, hmem->_usePinnedHost,
|
|
|
|
|
useHostToHost, useDeviceToDevice, useMemkindDefault, dmem->offset(), hmem->offset());
|
2016-02-12 17:39:44 -06:00
|
|
|
|
|
|
|
|
|
2017-04-24 11:53:31 -05:00
|
|
|
hmem->reset(numElements);
|
2016-02-13 01:15:23 -06:00
|
|
|
unsigned blocks = HipTest::setNumBlocks(blocksPerCU, threadsPerBlock, numElements);
|
2016-02-12 17:39:44 -06:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
assert(numElements <= dmem->maxNumElements());
|
|
|
|
|
assert(numElements <= hmem->maxNumElements());
|
2016-02-12 17:39:44 -06:00
|
|
|
|
2016-01-26 20:14:33 -06:00
|
|
|
|
2016-02-12 17:39:44 -06:00
|
|
|
if (useHostToHost) {
|
2016-02-12 18:23:55 -06:00
|
|
|
// Do some extra host-to-host copies here to mix things up:
|
2018-03-12 11:29:03 +05:30
|
|
|
HIPCHECK(hipMemcpy(hmem->A_hh, hmem->A_h(), sizeElements,
|
|
|
|
|
useMemkindDefault ? hipMemcpyDefault : hipMemcpyHostToHost));
|
|
|
|
|
HIPCHECK(hipMemcpy(hmem->B_hh, hmem->B_h(), sizeElements,
|
|
|
|
|
useMemkindDefault ? hipMemcpyDefault : hipMemcpyHostToHost));
|
2016-02-12 17:39:44 -06:00
|
|
|
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
HIPCHECK(hipMemcpy(dmem->A_d(), hmem->A_hh, sizeElements,
|
|
|
|
|
useMemkindDefault ? hipMemcpyDefault : hipMemcpyHostToDevice));
|
|
|
|
|
HIPCHECK(hipMemcpy(dmem->B_d(), hmem->B_hh, sizeElements,
|
|
|
|
|
useMemkindDefault ? hipMemcpyDefault : hipMemcpyHostToDevice));
|
2016-02-12 17:39:44 -06:00
|
|
|
} else {
|
2018-03-12 11:29:03 +05:30
|
|
|
HIPCHECK(hipMemcpy(dmem->A_d(), hmem->A_h(), sizeElements,
|
|
|
|
|
useMemkindDefault ? hipMemcpyDefault : hipMemcpyHostToDevice));
|
|
|
|
|
HIPCHECK(hipMemcpy(dmem->B_d(), hmem->B_h(), sizeElements,
|
|
|
|
|
useMemkindDefault ? hipMemcpyDefault : hipMemcpyHostToDevice));
|
2016-02-12 17:39:44 -06:00
|
|
|
}
|
|
|
|
|
|
2018-10-17 12:01:44 +05:30
|
|
|
hipLaunchKernelGGL(HipTest::vectorADD, dim3(blocks), dim3(threadsPerBlock), 0, 0,
|
2018-03-12 11:29:03 +05:30
|
|
|
static_cast<const T*>(dmem->A_d()), static_cast<const T*>(dmem->B_d()),
|
|
|
|
|
dmem->C_d(), numElements);
|
2016-02-12 17:39:44 -06:00
|
|
|
|
2016-02-12 18:23:55 -06:00
|
|
|
if (useDeviceToDevice) {
|
2017-04-24 11:53:31 -05:00
|
|
|
// Do an extra device-to-device copy here to mix things up:
|
2018-03-12 11:29:03 +05:30
|
|
|
HIPCHECK(hipMemcpy(dmem->C_dd(), dmem->C_d(), sizeElements,
|
|
|
|
|
useMemkindDefault ? hipMemcpyDefault : hipMemcpyDeviceToDevice));
|
2016-02-12 17:39:44 -06:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
// Destroy the original dmem->C_d():
|
|
|
|
|
HIPCHECK(hipMemset(dmem->C_d(), 0x5A, sizeElements));
|
2016-02-12 18:23:55 -06:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
HIPCHECK(hipMemcpy(hmem->C_h(), dmem->C_dd(), sizeElements,
|
|
|
|
|
useMemkindDefault ? hipMemcpyDefault : hipMemcpyDeviceToHost));
|
2016-02-12 18:23:55 -06:00
|
|
|
} else {
|
2018-03-12 11:29:03 +05:30
|
|
|
HIPCHECK(hipMemcpy(hmem->C_h(), dmem->C_d(), sizeElements,
|
|
|
|
|
useMemkindDefault ? hipMemcpyDefault : hipMemcpyDeviceToHost));
|
2016-02-12 18:23:55 -06:00
|
|
|
}
|
2016-02-12 17:39:44 -06:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
HIPCHECK(hipDeviceSynchronize());
|
2017-04-24 21:22:56 -05:00
|
|
|
HipTest::checkVectorADD(hmem->A_h(), hmem->B_h(), hmem->C_h(), numElements);
|
2017-04-24 11:53:31 -05:00
|
|
|
|
2016-02-12 17:39:44 -06:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
printf(" %s success\n", __func__);
|
2016-02-12 17:39:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-02-16 01:58:24 -06:00
|
|
|
//---
|
2018-03-12 11:29:03 +05:30
|
|
|
// Try all the 16 possible combinations to memcpytest2 - usePinnedHost, useHostToHost,
|
|
|
|
|
// useDeviceToDevice, useMemkindDefault
|
|
|
|
|
template <typename T>
|
|
|
|
|
void memcpytest2_for_type(size_t numElements) {
|
2016-02-13 01:15:23 -06:00
|
|
|
printSep();
|
|
|
|
|
|
This switches HIP from its currently convoluted macro + pfe based dispatch mechanism to a more natural one partially based on the existing module API. The basic idea is that HCC will always correctly emit __global__ functions: as empty-bodied stubs, on host, and as kernels, on device. It then becomes trivial to obtain the mangled name on host, at dispatch, from the function's address, and then to use the mangled name to retrieve the kernel. This should address all problems stemming from serialisation, dubious mismatches due to the manufactured functor, macro-isms et al. It also immediately enables support for generalised globals as a consequence of that being available in the module API. Finally, it will make debug much easier, since the actual names of the __global__ functions will automatically be used in traces etc. One detail is that due to how dispatch works now (hipLaunchKernel and hipLaunchKernelGGL are themselves variadic function templates which deduce the function type of the callee), in certain cases it may be necesssary to insert explicit casts to ensure that the variadic argument list selects a viable overload - this can be observed in some unit tests. Eventually we may be able to remove this limitation, but for now it does not appear terribly onerous. The code is not extremely HIPpie, nor is it fully optimised, but rather is intended as a starting point for the HIP team to make its own.
2017-11-01 15:09:59 +00:00
|
|
|
DeviceMemory<T> memD(numElements);
|
2018-03-12 11:29:03 +05:30
|
|
|
HostMemory<T> memU(numElements, 0 /*usePinnedHost*/);
|
|
|
|
|
HostMemory<T> memP(numElements, 1 /*usePinnedHost*/);
|
|
|
|
|
|
|
|
|
|
for (int usePinnedHost = 0; usePinnedHost <= 1; usePinnedHost++) {
|
|
|
|
|
for (int useHostToHost = 0; useHostToHost <= 1; useHostToHost++) { // TODO
|
|
|
|
|
for (int useDeviceToDevice = 0; useDeviceToDevice <= 1; useDeviceToDevice++) {
|
|
|
|
|
for (int useMemkindDefault = 0; useMemkindDefault <= 1; useMemkindDefault++) {
|
|
|
|
|
memcpytest2<T>(&memD, usePinnedHost ? &memP : &memU, numElements, useHostToHost,
|
|
|
|
|
useDeviceToDevice, useMemkindDefault);
|
2016-02-12 18:23:55 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-12 17:39:44 -06:00
|
|
|
|
2016-02-16 01:58:24 -06:00
|
|
|
//---
|
2018-03-12 11:29:03 +05:30
|
|
|
// Try many different sizes to memory copy.
|
|
|
|
|
template <typename T>
|
|
|
|
|
void memcpytest2_sizes(size_t maxElem = 0) {
|
2016-02-13 01:15:23 -06:00
|
|
|
printSep();
|
2018-03-12 11:29:03 +05:30
|
|
|
printf("test: %s<%s>\n", __func__, TYPENAME(T));
|
2016-02-13 01:15:23 -06:00
|
|
|
|
|
|
|
|
int deviceId;
|
|
|
|
|
HIPCHECK(hipGetDevice(&deviceId));
|
|
|
|
|
|
|
|
|
|
size_t free, total;
|
|
|
|
|
HIPCHECK(hipMemGetInfo(&free, &total));
|
|
|
|
|
|
|
|
|
|
if (maxElem == 0) {
|
2018-03-12 11:29:03 +05:30
|
|
|
maxElem = free / sizeof(T) / 8;
|
2016-02-13 01:15:23 -06:00
|
|
|
}
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
printf(
|
|
|
|
|
" device#%d: hipMemGetInfo: free=%zu (%4.2fMB) total=%zu (%4.2fMB) maxSize=%6.1fMB\n",
|
|
|
|
|
deviceId, free, (float)(free / 1024.0 / 1024.0), total, (float)(total / 1024.0 / 1024.0),
|
|
|
|
|
maxElem * sizeof(T) / 1024.0 / 1024.0);
|
|
|
|
|
HIPCHECK(hipDeviceReset());
|
This switches HIP from its currently convoluted macro + pfe based dispatch mechanism to a more natural one partially based on the existing module API. The basic idea is that HCC will always correctly emit __global__ functions: as empty-bodied stubs, on host, and as kernels, on device. It then becomes trivial to obtain the mangled name on host, at dispatch, from the function's address, and then to use the mangled name to retrieve the kernel. This should address all problems stemming from serialisation, dubious mismatches due to the manufactured functor, macro-isms et al. It also immediately enables support for generalised globals as a consequence of that being available in the module API. Finally, it will make debug much easier, since the actual names of the __global__ functions will automatically be used in traces etc. One detail is that due to how dispatch works now (hipLaunchKernel and hipLaunchKernelGGL are themselves variadic function templates which deduce the function type of the callee), in certain cases it may be necesssary to insert explicit casts to ensure that the variadic argument list selects a viable overload - this can be observed in some unit tests. Eventually we may be able to remove this limitation, but for now it does not appear terribly onerous. The code is not extremely HIPpie, nor is it fully optimised, but rather is intended as a starting point for the HIP team to make its own.
2017-11-01 15:09:59 +00:00
|
|
|
DeviceMemory<T> memD(maxElem);
|
2018-03-12 11:29:03 +05:30
|
|
|
HostMemory<T> memU(maxElem, 0 /*usePinnedHost*/);
|
|
|
|
|
HostMemory<T> memP(maxElem, 1 /*usePinnedHost*/);
|
2016-02-13 01:15:23 -06:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
for (size_t elem = 1; elem <= maxElem; elem *= 2) {
|
2017-04-24 12:51:17 -05:00
|
|
|
memcpytest2<T>(&memD, &memU, elem, 1, 1, 0); // unpinned host
|
|
|
|
|
memcpytest2<T>(&memD, &memP, elem, 1, 1, 0); // pinned host
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---
|
2018-03-12 11:29:03 +05:30
|
|
|
// Try many different sizes to memory copy.
|
|
|
|
|
template <typename T>
|
|
|
|
|
void memcpytest2_offsets(size_t maxElem, bool devOffsets, bool hostOffsets) {
|
2017-04-24 12:51:17 -05:00
|
|
|
printSep();
|
2018-03-12 11:29:03 +05:30
|
|
|
printf("test: %s<%s>\n", __func__, TYPENAME(T));
|
2017-04-24 12:51:17 -05:00
|
|
|
|
|
|
|
|
int deviceId;
|
|
|
|
|
HIPCHECK(hipGetDevice(&deviceId));
|
|
|
|
|
|
|
|
|
|
size_t free, total;
|
|
|
|
|
HIPCHECK(hipMemGetInfo(&free, &total));
|
|
|
|
|
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
printf(
|
|
|
|
|
" device#%d: hipMemGetInfo: free=%zu (%4.2fMB) total=%zu (%4.2fMB) maxSize=%6.1fMB\n",
|
|
|
|
|
deviceId, free, (float)(free / 1024.0 / 1024.0), total, (float)(total / 1024.0 / 1024.0),
|
|
|
|
|
maxElem * sizeof(T) / 1024.0 / 1024.0);
|
|
|
|
|
HIPCHECK(hipDeviceReset());
|
This switches HIP from its currently convoluted macro + pfe based dispatch mechanism to a more natural one partially based on the existing module API. The basic idea is that HCC will always correctly emit __global__ functions: as empty-bodied stubs, on host, and as kernels, on device. It then becomes trivial to obtain the mangled name on host, at dispatch, from the function's address, and then to use the mangled name to retrieve the kernel. This should address all problems stemming from serialisation, dubious mismatches due to the manufactured functor, macro-isms et al. It also immediately enables support for generalised globals as a consequence of that being available in the module API. Finally, it will make debug much easier, since the actual names of the __global__ functions will automatically be used in traces etc. One detail is that due to how dispatch works now (hipLaunchKernel and hipLaunchKernelGGL are themselves variadic function templates which deduce the function type of the callee), in certain cases it may be necesssary to insert explicit casts to ensure that the variadic argument list selects a viable overload - this can be observed in some unit tests. Eventually we may be able to remove this limitation, but for now it does not appear terribly onerous. The code is not extremely HIPpie, nor is it fully optimised, but rather is intended as a starting point for the HIP team to make its own.
2017-11-01 15:09:59 +00:00
|
|
|
DeviceMemory<T> memD(maxElem);
|
2018-03-12 11:29:03 +05:30
|
|
|
HostMemory<T> memU(maxElem, 0 /*usePinnedHost*/);
|
|
|
|
|
HostMemory<T> memP(maxElem, 1 /*usePinnedHost*/);
|
2017-04-24 12:51:17 -05:00
|
|
|
|
|
|
|
|
size_t elem = maxElem / 2;
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
for (int offset = 0; offset < 512; offset++) {
|
|
|
|
|
assert(elem + offset < maxElem);
|
2017-04-24 21:22:56 -05:00
|
|
|
if (devOffsets) {
|
|
|
|
|
memD.offset(offset);
|
|
|
|
|
}
|
|
|
|
|
if (hostOffsets) {
|
|
|
|
|
memU.offset(offset);
|
|
|
|
|
memP.offset(offset);
|
|
|
|
|
}
|
2017-04-24 12:51:17 -05:00
|
|
|
memcpytest2<T>(&memD, &memU, elem, 1, 1, 0); // unpinned host
|
|
|
|
|
memcpytest2<T>(&memD, &memP, elem, 1, 1, 0); // pinned host
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
for (int offset = 512; offset < elem; offset *= 2) {
|
|
|
|
|
assert(elem + offset < maxElem);
|
2017-04-24 21:22:56 -05:00
|
|
|
if (devOffsets) {
|
|
|
|
|
memD.offset(offset);
|
|
|
|
|
}
|
|
|
|
|
if (hostOffsets) {
|
|
|
|
|
memU.offset(offset);
|
|
|
|
|
memP.offset(offset);
|
|
|
|
|
}
|
2017-04-24 12:51:17 -05:00
|
|
|
memcpytest2<T>(&memD, &memU, elem, 1, 1, 0); // unpinned host
|
|
|
|
|
memcpytest2<T>(&memD, &memP, elem, 1, 1, 0); // pinned host
|
2016-02-13 01:15:23 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-02-16 01:58:24 -06:00
|
|
|
//---
|
2018-03-12 11:29:03 +05:30
|
|
|
// Create multiple threads to stress multi-thread locking behavior in the
|
|
|
|
|
// allocation/deallocation/tracking logic:
|
|
|
|
|
template <typename T>
|
|
|
|
|
void multiThread_1(bool serialize, bool usePinnedHost) {
|
2016-02-13 01:15:23 -06:00
|
|
|
printSep();
|
2018-03-12 11:29:03 +05:30
|
|
|
printf("test: %s<%s> serialize=%d usePinnedHost=%d\n", __func__, TYPENAME(T), serialize,
|
|
|
|
|
usePinnedHost);
|
This switches HIP from its currently convoluted macro + pfe based dispatch mechanism to a more natural one partially based on the existing module API. The basic idea is that HCC will always correctly emit __global__ functions: as empty-bodied stubs, on host, and as kernels, on device. It then becomes trivial to obtain the mangled name on host, at dispatch, from the function's address, and then to use the mangled name to retrieve the kernel. This should address all problems stemming from serialisation, dubious mismatches due to the manufactured functor, macro-isms et al. It also immediately enables support for generalised globals as a consequence of that being available in the module API. Finally, it will make debug much easier, since the actual names of the __global__ functions will automatically be used in traces etc. One detail is that due to how dispatch works now (hipLaunchKernel and hipLaunchKernelGGL are themselves variadic function templates which deduce the function type of the callee), in certain cases it may be necesssary to insert explicit casts to ensure that the variadic argument list selects a viable overload - this can be observed in some unit tests. Eventually we may be able to remove this limitation, but for now it does not appear terribly onerous. The code is not extremely HIPpie, nor is it fully optimised, but rather is intended as a starting point for the HIP team to make its own.
2017-11-01 15:09:59 +00:00
|
|
|
DeviceMemory<T> memD(N);
|
|
|
|
|
HostMemory<T> mem1(N, usePinnedHost);
|
|
|
|
|
HostMemory<T> mem2(N, usePinnedHost);
|
2017-04-24 11:53:31 -05:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
std::thread t1(memcpytest2<T>, &memD, &mem1, N, 0, 0, 0);
|
2016-02-13 01:15:23 -06:00
|
|
|
if (serialize) {
|
|
|
|
|
t1.join();
|
|
|
|
|
}
|
|
|
|
|
|
This switches HIP from its currently convoluted macro + pfe based dispatch mechanism to a more natural one partially based on the existing module API. The basic idea is that HCC will always correctly emit __global__ functions: as empty-bodied stubs, on host, and as kernels, on device. It then becomes trivial to obtain the mangled name on host, at dispatch, from the function's address, and then to use the mangled name to retrieve the kernel. This should address all problems stemming from serialisation, dubious mismatches due to the manufactured functor, macro-isms et al. It also immediately enables support for generalised globals as a consequence of that being available in the module API. Finally, it will make debug much easier, since the actual names of the __global__ functions will automatically be used in traces etc. One detail is that due to how dispatch works now (hipLaunchKernel and hipLaunchKernelGGL are themselves variadic function templates which deduce the function type of the callee), in certain cases it may be necesssary to insert explicit casts to ensure that the variadic argument list selects a viable overload - this can be observed in some unit tests. Eventually we may be able to remove this limitation, but for now it does not appear terribly onerous. The code is not extremely HIPpie, nor is it fully optimised, but rather is intended as a starting point for the HIP team to make its own.
2017-11-01 15:09:59 +00:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
std::thread t2(memcpytest2<T>, &memD, &mem2, N, 0, 0, 0);
|
2016-02-13 01:15:23 -06:00
|
|
|
if (serialize) {
|
|
|
|
|
t2.join();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!serialize) {
|
|
|
|
|
t1.join();
|
|
|
|
|
t2.join();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
int main(int argc, char* argv[]) {
|
2016-02-12 17:39:44 -06:00
|
|
|
HipTest::parseStandardArguments(argc, argv, true);
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
printf("info: set device to %d\n", p_gpuDevice);
|
2016-02-15 05:19:52 -06:00
|
|
|
HIPCHECK(hipSetDevice(p_gpuDevice));
|
|
|
|
|
|
2016-02-12 17:39:44 -06:00
|
|
|
|
2016-02-13 01:15:23 -06:00
|
|
|
if (p_tests & 0x1) {
|
2018-03-12 11:29:03 +05:30
|
|
|
printf("\n\n=== tests&1 (types and different memcpy kinds (H2D, D2H, H2H, D2D)\n");
|
|
|
|
|
HIPCHECK(hipDeviceReset());
|
2016-03-07 17:15:48 -06:00
|
|
|
memcpytest2_for_type<float>(N);
|
|
|
|
|
memcpytest2_for_type<double>(N);
|
|
|
|
|
memcpytest2_for_type<char>(N);
|
|
|
|
|
memcpytest2_for_type<int>(N);
|
2018-03-12 11:29:03 +05:30
|
|
|
printf("===\n\n\n");
|
2016-02-13 01:15:23 -06:00
|
|
|
}
|
2016-02-12 17:39:44 -06:00
|
|
|
|
2016-03-07 17:15:48 -06:00
|
|
|
|
2016-02-13 01:15:23 -06:00
|
|
|
if (p_tests & 0x2) {
|
2017-04-24 11:53:31 -05:00
|
|
|
// Some tests around the 64KB boundary which have historically shown issues:
|
2018-03-12 11:29:03 +05:30
|
|
|
printf("\n\n=== tests&0x2 (64KB boundary)\n");
|
|
|
|
|
size_t maxElem = 32 * 1024 * 1024;
|
This switches HIP from its currently convoluted macro + pfe based dispatch mechanism to a more natural one partially based on the existing module API. The basic idea is that HCC will always correctly emit __global__ functions: as empty-bodied stubs, on host, and as kernels, on device. It then becomes trivial to obtain the mangled name on host, at dispatch, from the function's address, and then to use the mangled name to retrieve the kernel. This should address all problems stemming from serialisation, dubious mismatches due to the manufactured functor, macro-isms et al. It also immediately enables support for generalised globals as a consequence of that being available in the module API. Finally, it will make debug much easier, since the actual names of the __global__ functions will automatically be used in traces etc. One detail is that due to how dispatch works now (hipLaunchKernel and hipLaunchKernelGGL are themselves variadic function templates which deduce the function type of the callee), in certain cases it may be necesssary to insert explicit casts to ensure that the variadic argument list selects a viable overload - this can be observed in some unit tests. Eventually we may be able to remove this limitation, but for now it does not appear terribly onerous. The code is not extremely HIPpie, nor is it fully optimised, but rather is intended as a starting point for the HIP team to make its own.
2017-11-01 15:09:59 +00:00
|
|
|
DeviceMemory<float> memD(maxElem);
|
2018-03-12 11:29:03 +05:30
|
|
|
HostMemory<float> memU(maxElem, 0 /*usePinnedHost*/);
|
|
|
|
|
HostMemory<float> memP(maxElem, 0 /*usePinnedHost*/);
|
2016-03-07 17:15:48 -06:00
|
|
|
// These all pass:
|
2018-03-12 11:29:03 +05:30
|
|
|
memcpytest2<float>(&memD, &memP, 15 * 1024 * 1024, 0, 0, 0);
|
|
|
|
|
memcpytest2<float>(&memD, &memP, 16 * 1024 * 1024, 0, 0, 0);
|
|
|
|
|
memcpytest2<float>(&memD, &memP, 16 * 1024 * 1024 + 16 * 1024, 0, 0, 0);
|
2017-04-24 11:53:31 -05:00
|
|
|
|
2016-03-07 17:15:48 -06:00
|
|
|
// Just over 64MB:
|
2018-03-12 11:29:03 +05:30
|
|
|
memcpytest2<float>(&memD, &memP, 16 * 1024 * 1024 + 512 * 1024, 0, 0, 0);
|
|
|
|
|
memcpytest2<float>(&memD, &memP, 17 * 1024 * 1024 + 1024, 0, 0, 0);
|
|
|
|
|
memcpytest2<float>(&memD, &memP, 32 * 1024 * 1024, 0, 0, 0);
|
|
|
|
|
memcpytest2<float>(&memD, &memU, 32 * 1024 * 1024, 0, 0, 0);
|
|
|
|
|
memcpytest2<float>(&memD, &memP, 32 * 1024 * 1024, 1, 1, 0);
|
|
|
|
|
memcpytest2<float>(&memD, &memP, 32 * 1024 * 1024, 1, 1, 0);
|
2016-02-13 01:15:23 -06:00
|
|
|
}
|
2016-02-12 18:23:55 -06:00
|
|
|
|
2016-03-07 17:15:48 -06:00
|
|
|
|
2016-02-13 01:15:23 -06:00
|
|
|
if (p_tests & 0x4) {
|
2018-03-12 11:29:03 +05:30
|
|
|
printf("\n\n=== tests&4 (test sizes)\n");
|
|
|
|
|
HIPCHECK(hipDeviceReset());
|
2017-04-24 12:51:17 -05:00
|
|
|
memcpytest2_sizes<float>(0);
|
2016-02-13 01:15:23 -06:00
|
|
|
printSep();
|
2017-04-24 12:51:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-02-13 01:15:23 -06:00
|
|
|
if (p_tests & 0x8) {
|
2018-03-12 11:29:03 +05:30
|
|
|
printf("\n\n=== tests&8\n");
|
|
|
|
|
HIPCHECK(hipDeviceReset());
|
2016-02-13 01:15:23 -06:00
|
|
|
printSep();
|
2016-02-25 19:17:28 -06:00
|
|
|
|
|
|
|
|
// Simplest cases: serialize the threads, and also used pinned memory:
|
|
|
|
|
// This verifies that the sub-calls to memcpytest2 are correct.
|
This switches HIP from its currently convoluted macro + pfe based dispatch mechanism to a more natural one partially based on the existing module API. The basic idea is that HCC will always correctly emit __global__ functions: as empty-bodied stubs, on host, and as kernels, on device. It then becomes trivial to obtain the mangled name on host, at dispatch, from the function's address, and then to use the mangled name to retrieve the kernel. This should address all problems stemming from serialisation, dubious mismatches due to the manufactured functor, macro-isms et al. It also immediately enables support for generalised globals as a consequence of that being available in the module API. Finally, it will make debug much easier, since the actual names of the __global__ functions will automatically be used in traces etc. One detail is that due to how dispatch works now (hipLaunchKernel and hipLaunchKernelGGL are themselves variadic function templates which deduce the function type of the callee), in certain cases it may be necesssary to insert explicit casts to ensure that the variadic argument list selects a viable overload - this can be observed in some unit tests. Eventually we may be able to remove this limitation, but for now it does not appear terribly onerous. The code is not extremely HIPpie, nor is it fully optimised, but rather is intended as a starting point for the HIP team to make its own.
2017-11-01 15:09:59 +00:00
|
|
|
multiThread_1<float>(true, true);
|
2016-02-25 19:17:28 -06:00
|
|
|
|
|
|
|
|
// Serialize, but use unpinned memory to stress the unpinned memory xfer path.
|
|
|
|
|
multiThread_1<float>(true, false);
|
|
|
|
|
|
|
|
|
|
// Remove serialization, so two threads are performing memory copies in parallel.
|
2016-02-13 03:18:01 -06:00
|
|
|
multiThread_1<float>(false, true);
|
2016-02-25 19:17:28 -06:00
|
|
|
|
|
|
|
|
// Remove serialization, and use unpinned.
|
2018-03-12 11:29:03 +05:30
|
|
|
multiThread_1<float>(false, false); // TODO
|
|
|
|
|
printf("===\n\n\n");
|
2016-02-13 01:15:23 -06:00
|
|
|
}
|
2016-02-12 17:39:44 -06:00
|
|
|
|
2016-03-07 17:15:48 -06:00
|
|
|
|
2017-04-24 16:02:22 -05:00
|
|
|
if (p_tests & 0x10) {
|
2018-03-12 11:29:03 +05:30
|
|
|
printf("\n\n=== tests&0x10 (test device offsets)\n");
|
|
|
|
|
HIPCHECK(hipDeviceReset());
|
|
|
|
|
size_t maxSize = 256 * 1024;
|
|
|
|
|
memcpytest2_offsets<char>(maxSize, true, false);
|
|
|
|
|
memcpytest2_offsets<float>(maxSize, true, false);
|
2017-04-24 21:22:56 -05:00
|
|
|
memcpytest2_offsets<double>(maxSize, true, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (p_tests & 0x20) {
|
2018-03-12 11:29:03 +05:30
|
|
|
printf("\n\n=== tests&0x10 (test device offsets)\n");
|
|
|
|
|
HIPCHECK(hipDeviceReset());
|
|
|
|
|
size_t maxSize = 256 * 1024;
|
|
|
|
|
memcpytest2_offsets<char>(maxSize, false, true);
|
|
|
|
|
memcpytest2_offsets<float>(maxSize, false, true);
|
2017-04-24 21:22:56 -05:00
|
|
|
memcpytest2_offsets<double>(maxSize, false, true);
|
2017-04-24 16:02:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-01-26 20:14:33 -06:00
|
|
|
passed();
|
|
|
|
|
}
|