Dosyalar
rocm-systems/tests/src/hipPeerToPeer_simple.cpp
T

126 satır
4.2 KiB
C++
Ham Normal Görünüm Geçmiş

2016-04-07 15:51:08 -05: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.
*/
// Simple test for memset.
// Also serves as a template for other tests.
#include "hip_runtime.h"
#include "test_common.h"
2016-04-09 05:03:08 -05:00
bool p_memcpyWithPeer = false; // use the peer device for the P2P copy
bool p_mirrorPeers = false; // in addition to mapping current to peer space, map peer to current space.
int p_peerDevice = -1; // explicly specify which peer to use, else use p_gpuDevice + 1.
2016-04-07 15:51:08 -05:00
2016-04-09 04:47:07 -05:00
void parseMyArguments(int argc, char *argv[])
2016-04-07 15:51:08 -05:00
{
2016-04-09 04:47:07 -05:00
int more_argc = HipTest::parseStandardArguments(argc, argv, false);
// parse args for this test:
for (int i = 1; i < more_argc; i++) {
const char *arg = argv[i];
if (!strcmp(arg, "--memcpyWithPeer")) {
p_memcpyWithPeer = true;
2016-04-09 05:03:08 -05:00
} else if (!strcmp(arg, "--mirrorPeers")) {
p_mirrorPeers = true;
} else if (!strcmp(arg, "--peerDevice")) {
if (++i >= argc || !HipTest::parseInt(argv[i], &p_peerDevice)) {
failed("Bad peerDevice argument");
}
2016-04-09 04:47:07 -05:00
} else {
failed("Bad argument '%s'", arg);
}
};
};
2016-04-07 15:51:08 -05:00
2016-04-09 05:03:08 -05:00
2016-04-09 04:47:07 -05:00
int main(int argc, char *argv[])
{
parseMyArguments(argc, argv);
2016-04-07 15:51:08 -05:00
int deviceCnt;
HIPCHECK(hipGetDeviceCount(&deviceCnt));
2016-04-09 05:03:08 -05:00
int currentDevice = p_gpuDevice;
int peerDevice = (p_peerDevice == -1) ? ((currentDevice + 1) % deviceCnt) : p_peerDevice;
2016-04-07 15:51:08 -05:00
2016-04-09 05:03:08 -05:00
printf ("N=%zu device=%d peerDevice=%d (%d devices total)\n", N, currentDevice, peerDevice, deviceCnt);
2016-04-07 15:51:08 -05:00
// Must be on a multi-gpu system:
2016-04-09 05:03:08 -05:00
assert (currentDevice != peerDevice);
2016-04-07 15:51:08 -05:00
int canAccessPeer;
2016-04-09 05:03:08 -05:00
HIPCHECK(hipDeviceCanAccessPeer(&canAccessPeer, currentDevice, peerDevice));
printf ("dev#%d canAccessPeer:#%d=%d\n", currentDevice, peerDevice, canAccessPeer);
2016-04-07 15:51:08 -05:00
assert(canAccessPeer);
2016-04-09 05:03:08 -05:00
HIPCHECK(hipSetDevice(currentDevice));
2016-04-07 15:51:08 -05:00
HIPCHECK(hipDeviceEnablePeerAccess(peerDevice, 0));
2016-04-09 05:03:08 -05:00
if (p_mirrorPeers) {
int canAccessPeer;
HIPCHECK(hipDeviceCanAccessPeer(&canAccessPeer, peerDevice, currentDevice));
assert(canAccessPeer);
HIPCHECK(hipSetDevice(peerDevice));
HIPCHECK(hipDeviceEnablePeerAccess(currentDevice, 0));
}
size_t Nbytes = N*sizeof(char);
2016-04-07 15:51:08 -05:00
char *A_d0, *A_d1;
char *A_h;
2016-04-07 15:51:08 -05:00
A_h = (char*)malloc(Nbytes);
2016-04-07 15:51:08 -05:00
2016-04-09 04:47:07 -05:00
// allocate and initialize memory on device0
2016-04-09 05:03:08 -05:00
HIPCHECK (hipSetDevice(currentDevice));
2016-04-07 15:51:08 -05:00
HIPCHECK (hipMalloc(&A_d0, Nbytes) );
2016-04-09 04:47:07 -05:00
HIPCHECK ( hipMemset(A_d0, memsetval, Nbytes) );
// allocate and initialize memory on peer device
HIPCHECK (hipSetDevice(peerDevice));
HIPCHECK (hipMalloc(&A_d1, Nbytes) );
HIPCHECK ( hipMemset(A_d1, 0x13, Nbytes) );
2016-04-07 15:51:08 -05:00
// Device0 push to device1, using P2P:
2016-04-09 05:03:08 -05:00
HIPCHECK (hipSetDevice(p_memcpyWithPeer ? peerDevice : currentDevice));
2016-04-09 04:47:07 -05:00
HIPCHECK (hipMemcpy(A_d1, A_d0, Nbytes, hipMemcpyDefault));
2016-04-07 15:51:08 -05:00
// Copy data back to host:
2016-04-09 04:47:07 -05:00
HIPCHECK (hipSetDevice(peerDevice));
HIPCHECK (hipMemcpy(A_h, A_d1, Nbytes, hipMemcpyDeviceToHost));
2016-04-07 15:51:08 -05:00
// Check host data:
for (int i=0; i<N; i++) {
if (A_h[i] != memsetval) {
2016-04-09 04:47:07 -05:00
failed("mismatch at index:%d computed:0x%02x, golden memsetval:0x%02x\n", i, (int)A_h[i], (int)memsetval);
2016-04-07 15:51:08 -05:00
}
}
passed();
}