// P2P Test by Greg Gutmann
// https://codingbyexample.com/2020/09/14/p2p-memcpy-with-nvlink/
 
#include "stdio.h"
#include "stdint.h"
 
int main()
{
    // GPUs
    int gpuid_0 = 0;
    int gpuid_1 = 1;
 
    // Memory Copy Size
    uint32_t size = pow(2, 26); // 2^26 = 67MB
 
    // Allocate Memory
    uint32_t* dev_0;
    cudaSetDevice(gpuid_0);
    cudaMalloc((void**)&dev_0, size);
 
    uint32_t* dev_1;
    cudaSetDevice(gpuid_1);
    cudaMalloc((void**)&dev_1, size);
 
    //Check for peer access between participating GPUs: 
    int can_access_peer_0_1;
    int can_access_peer_1_0;
    cudaDeviceCanAccessPeer(&can_access_peer_0_1, gpuid_0, gpuid_1);
    cudaDeviceCanAccessPeer(&can_access_peer_1_0, gpuid_1, gpuid_0);
    printf("cudaDeviceCanAccessPeer(%d->%d): %d\n", gpuid_0, gpuid_1, can_access_peer_0_1);
    printf("cudaDeviceCanAccessPeer(%d->%d): %d\n", gpuid_1, gpuid_0, can_access_peer_1_0);
 
    if (can_access_peer_0_1 && can_access_peer_1_0) {
        // Enable P2P Access
        cudaSetDevice(gpuid_0);
        cudaDeviceEnablePeerAccess(gpuid_1, 0);
        cudaSetDevice(gpuid_1);
        cudaDeviceEnablePeerAccess(gpuid_0, 0);
    }
 
    // Init Timing Data
    uint32_t repeat = 10;
    cudaEvent_t start;
    cudaEvent_t stop;
    cudaEventCreate(&start);
    cudaEventCreate(&stop);
 
    // Init Stream
    cudaStream_t stream;
    cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking);
 
    // ~~ Start Test ~~
    cudaEventRecord(start, stream);
 
    //Do a P2P memcpy
    for (int i = 0; i < repeat; ++i) {
        cudaMemcpyAsync(dev_0, dev_1, size, cudaMemcpyDeviceToDevice, stream);
    }
 
    cudaEventRecord(stop, stream);
    cudaStreamSynchronize(stream);
    // ~~ End of Test ~~
 
    // Check Timing & Performance
    float time_ms;
    cudaEventElapsedTime(&time_ms, start, stop);
    double time_s = time_ms / 1e3;
 
    double gb = size * repeat / (double)1e9;
    double bandwidth = gb / time_s;
 
    printf("Seconds: %f\n", time_s);
    printf("Unidirectional Bandwidth: %f (GB/s)\n", bandwidth);
 
    if (can_access_peer_0_1 && can_access_peer_1_0) {
        // Shutdown P2P Settings
        cudaSetDevice(gpuid_0);
        cudaDeviceDisablePeerAccess(gpuid_1);
        cudaSetDevice(gpuid_1);
        cudaDeviceDisablePeerAccess(gpuid_0);
    }
 
    // Clean Up
    cudaFree(dev_0);
    cudaFree(dev_1);
 
    cudaEventDestroy(start);
    cudaEventDestroy(stop);
    cudaStreamDestroy(stream);
}

	.file	"tmpxft_00223074_00000000-6_memcpy_with_nvlink.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2031:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2031:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"cudaDeviceCanAccessPeer(%d->%d): %d\n"
.LC3:
	.string	"Seconds: %f\n"
.LC4:
	.string	"Unidirectional Bandwidth: %f (GB/s)\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	xorl	%edi, %edi
	leaq	.LC0(%rip), %rbx
	subq	$64, %rsp
	.cfi_def_cfa_offset 80
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	call	cudaSetDevice@PLT
	movl	$67108864, %esi
	leaq	16(%rsp), %rdi
	call	cudaMalloc@PLT
	movl	$1, %edi
	call	cudaSetDevice@PLT
	leaq	24(%rsp), %rdi
	movl	$67108864, %esi
	call	cudaMalloc@PLT
	xorl	%esi, %esi
	leaq	4(%rsp), %rdi
	movl	$1, %edx
	call	cudaDeviceCanAccessPeer@PLT
	xorl	%edx, %edx
	leaq	8(%rsp), %rdi
	movl	$1, %esi
	call	cudaDeviceCanAccessPeer@PLT
	movl	4(%rsp), %r8d
	xorl	%edx, %edx
	movq	%rbx, %rsi
	movl	$1, %ecx
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	8(%rsp), %r8d
	xorl	%ecx, %ecx
	xorl	%eax, %eax
	movl	$1, %edx
	movq	%rbx, %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	cmpl	$0, 4(%rsp)
	je	.L3
	cmpl	$0, 8(%rsp)
	je	.L3
	xorl	%edi, %edi
	call	cudaSetDevice@PLT
	xorl	%esi, %esi
	movl	$1, %edi
	call	cudaDeviceEnablePeerAccess@PLT
	movl	$1, %edi
	call	cudaSetDevice@PLT
	xorl	%esi, %esi
	xorl	%edi, %edi
	call	cudaDeviceEnablePeerAccess@PLT
.L3:
	leaq	32(%rsp), %rdi
	movl	$10, %ebx
	call	cudaEventCreate@PLT
	leaq	40(%rsp), %rdi
	call	cudaEventCreate@PLT
	leaq	48(%rsp), %rdi
	movl	$1, %esi
	call	cudaStreamCreateWithFlags@PLT
	movq	48(%rsp), %rsi
	movq	32(%rsp), %rdi
	call	cudaEventRecord@PLT
.L4:
	movq	48(%rsp), %r8
	movq	24(%rsp), %rsi
	movl	$3, %ecx
	movl	$67108864, %edx
	movq	16(%rsp), %rdi
	call	cudaMemcpyAsync@PLT
	decl	%ebx
	jne	.L4
	movq	48(%rsp), %rsi
	movq	40(%rsp), %rdi
	call	cudaEventRecord@PLT
	movq	48(%rsp), %rdi
	call	cudaStreamSynchronize@PLT
	movq	40(%rsp), %rdx
	movq	32(%rsp), %rsi
	leaq	12(%rsp), %rdi
	call	cudaEventElapsedTime@PLT
	movsd	.LC2(%rip), %xmm1
	cvtss2sd	12(%rsp), %xmm0
	movb	$1, %al
	divsd	.LC1(%rip), %xmm0
	leaq	.LC3(%rip), %rsi
	movl	$2, %edi
	divsd	%xmm0, %xmm1
	movq	%xmm1, %rbx
	call	__printf_chk@PLT
	movq	%rbx, %xmm0
	leaq	.LC4(%rip), %rsi
	movb	$1, %al
	movl	$2, %edi
	call	__printf_chk@PLT
	cmpl	$0, 4(%rsp)
	je	.L5
	cmpl	$0, 8(%rsp)
	je	.L5
	xorl	%edi, %edi
	call	cudaSetDevice@PLT
	movl	$1, %edi
	call	cudaDeviceDisablePeerAccess@PLT
	movl	$1, %edi
	call	cudaSetDevice@PLT
	xorl	%edi, %edi
	call	cudaDeviceDisablePeerAccess@PLT
.L5:
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	32(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	40(%rsp), %rdi
	call	cudaEventDestroy@PLT
	movq	48(%rsp), %rdi
	call	cudaStreamDestroy@PLT
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L6
	call	__stack_chk_fail@PLT
.L6:
	addq	$64, %rsp
	.cfi_def_cfa_offset 16
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2054:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2054:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC1:
	.long	0
	.long	1083129856
	.align 8
.LC2:
	.long	-500134854
	.long	1072003470
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

// P2P Test by Greg Gutmann
// https://codingbyexample.com/2020/09/14/p2p-memcpy-with-nvlink/
 

#include <hip/hip_runtime.h>
#include "stdio.h"
#include "stdint.h"
 
int main()
{
    // GPUs
    int gpuid_0 = 0;
    int gpuid_1 = 1;
 
    // Memory Copy Size
    uint32_t size = pow(2, 26); // 2^26 = 67MB
 
    // Allocate Memory
    uint32_t* dev_0;
    hipSetDevice(gpuid_0);
    hipMalloc((void**)&dev_0, size);
 
    uint32_t* dev_1;
    hipSetDevice(gpuid_1);
    hipMalloc((void**)&dev_1, size);
 
    //Check for peer access between participating GPUs: 
    int can_access_peer_0_1;
    int can_access_peer_1_0;
    hipDeviceCanAccessPeer(&can_access_peer_0_1, gpuid_0, gpuid_1);
    hipDeviceCanAccessPeer(&can_access_peer_1_0, gpuid_1, gpuid_0);
    printf("cudaDeviceCanAccessPeer(%d->%d): %d\n", gpuid_0, gpuid_1, can_access_peer_0_1);
    printf("cudaDeviceCanAccessPeer(%d->%d): %d\n", gpuid_1, gpuid_0, can_access_peer_1_0);
 
    if (can_access_peer_0_1 && can_access_peer_1_0) {
        // Enable P2P Access
        hipSetDevice(gpuid_0);
        hipDeviceEnablePeerAccess(gpuid_1, 0);
        hipSetDevice(gpuid_1);
        hipDeviceEnablePeerAccess(gpuid_0, 0);
    }
 
    // Init Timing Data
    uint32_t repeat = 10;
    hipEvent_t start;
    hipEvent_t stop;
    hipEventCreate(&start);
    hipEventCreate(&stop);
 
    // Init Stream
    hipStream_t stream;
    hipStreamCreateWithFlags(&stream, hipStreamNonBlocking);
 
    // ~~ Start Test ~~
    hipEventRecord(start, stream);
 
    //Do a P2P memcpy
    for (int i = 0; i < repeat; ++i) {
        hipMemcpyAsync(dev_0, dev_1, size, hipMemcpyDeviceToDevice, stream);
    }
 
    hipEventRecord(stop, stream);
    hipStreamSynchronize(stream);
    // ~~ End of Test ~~
 
    // Check Timing & Performance
    float time_ms;
    hipEventElapsedTime(&time_ms, start, stop);
    double time_s = time_ms / 1e3;
 
    double gb = size * repeat / (double)1e9;
    double bandwidth = gb / time_s;
 
    printf("Seconds: %f\n", time_s);
    printf("Unidirectional Bandwidth: %f (GB/s)\n", bandwidth);
 
    if (can_access_peer_0_1 && can_access_peer_1_0) {
        // Shutdown P2P Settings
        hipSetDevice(gpuid_0);
        hipDeviceDisablePeerAccess(gpuid_1);
        hipSetDevice(gpuid_1);
        hipDeviceDisablePeerAccess(gpuid_0);
    }
 
    // Clean Up
    hipFree(dev_0);
    hipFree(dev_1);
 
    hipEventDestroy(start);
    hipEventDestroy(stop);
    hipStreamDestroy(stream);
}

	.text
	.file	"memcpy_with_nvlink.hip"
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI0_0:
	.quad	0x408f400000000000              # double 1000
.LCPI0_1:
	.quad	0x3fe5798ee2308c3a              # double 0.67108864000000001
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%r14
	.cfi_def_cfa_offset 16
	pushq	%rbx
	.cfi_def_cfa_offset 24
	subq	$72, %rsp
	.cfi_def_cfa_offset 96
	.cfi_offset %rbx, -24
	.cfi_offset %r14, -16
	xorl	%edi, %edi
	callq	hipSetDevice
	leaq	48(%rsp), %rdi
	movl	$67108864, %esi                 # imm = 0x4000000
	callq	hipMalloc
	movl	$1, %edi
	callq	hipSetDevice
	leaq	40(%rsp), %rdi
	movl	$67108864, %esi                 # imm = 0x4000000
	callq	hipMalloc
	leaq	28(%rsp), %rbx
	movq	%rbx, %rdi
	xorl	%esi, %esi
	movl	$1, %edx
	callq	hipDeviceCanAccessPeer
	leaq	4(%rsp), %r14
	movq	%r14, %rdi
	movl	$1, %esi
	xorl	%edx, %edx
	callq	hipDeviceCanAccessPeer
	movl	(%rbx), %ecx
	movl	$.L.str, %edi
	xorl	%esi, %esi
	movl	$1, %edx
	xorl	%eax, %eax
	callq	printf
	movl	(%r14), %ecx
	movl	$.L.str, %edi
	movl	$1, %esi
	xorl	%edx, %edx
	xorl	%eax, %eax
	callq	printf
	cmpl	$0, (%rbx)
	je	.LBB0_3
# %bb.1:
	cmpl	$0, 4(%rsp)
	je	.LBB0_3
# %bb.2:
	xorl	%edi, %edi
	callq	hipSetDevice
	movl	$1, %edi
	xorl	%esi, %esi
	callq	hipDeviceEnablePeerAccess
	movl	$1, %edi
	callq	hipSetDevice
	xorl	%edi, %edi
	xorl	%esi, %esi
	callq	hipDeviceEnablePeerAccess
.LBB0_3:
	leaq	32(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventCreate
	leaq	16(%rsp), %rdi
	callq	hipEventCreate
	leaq	8(%rsp), %r14
	movq	%r14, %rdi
	movl	$1, %esi
	callq	hipStreamCreateWithFlags
	movq	(%rbx), %rdi
	movq	(%r14), %rsi
	callq	hipEventRecord
	movl	$10, %ebx
.LBB0_4:                                # =>This Inner Loop Header: Depth=1
	movq	48(%rsp), %rdi
	movq	40(%rsp), %rsi
	movq	8(%rsp), %r8
	movl	$67108864, %edx                 # imm = 0x4000000
	movl	$3, %ecx
	callq	hipMemcpyAsync
	decl	%ebx
	jne	.LBB0_4
# %bb.5:
	movq	16(%rsp), %rdi
	movq	8(%rsp), %rsi
	callq	hipEventRecord
	movq	8(%rsp), %rdi
	callq	hipStreamSynchronize
	movq	32(%rsp), %rsi
	movq	16(%rsp), %rdx
	leaq	68(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipEventElapsedTime
	cvtss2sd	(%rbx), %xmm0
	divsd	.LCPI0_0(%rip), %xmm0
	movsd	.LCPI0_1(%rip), %xmm1           # xmm1 = mem[0],zero
	divsd	%xmm0, %xmm1
	movsd	%xmm1, 56(%rsp)                 # 8-byte Spill
	movl	$.L.str.1, %edi
	movb	$1, %al
	callq	printf
	movl	$.L.str.2, %edi
	movsd	56(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	movb	$1, %al
	callq	printf
	cmpl	$0, 28(%rsp)
	je	.LBB0_8
# %bb.6:
	cmpl	$0, 4(%rsp)
	je	.LBB0_8
# %bb.7:
	xorl	%edi, %edi
	callq	hipSetDevice
	movl	$1, %edi
	callq	hipDeviceDisablePeerAccess
	movl	$1, %edi
	callq	hipSetDevice
	xorl	%edi, %edi
	callq	hipDeviceDisablePeerAccess
.LBB0_8:
	movq	48(%rsp), %rdi
	callq	hipFree
	movq	40(%rsp), %rdi
	callq	hipFree
	movq	32(%rsp), %rdi
	callq	hipEventDestroy
	movq	16(%rsp), %rdi
	callq	hipEventDestroy
	movq	8(%rsp), %rdi
	callq	hipStreamDestroy
	xorl	%eax, %eax
	addq	$72, %rsp
	.cfi_def_cfa_offset 24
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"cudaDeviceCanAccessPeer(%d->%d): %d\n"
	.size	.L.str, 37

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Seconds: %f\n"
	.size	.L.str.1, 13

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"Unidirectional Bandwidth: %f (GB/s)\n"
	.size	.L.str.2, 37

	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
