// ***************************************************************************
// Assignment #3
// Name: Yujin Yoshimura
// Parallel Programming Date: March 12, 2020
// ***************************************************************************
// References:
//
// NVIDIA CUDA Toolkit Documentation
// https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__DEVICE.html
// https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#features-and-technical-specifications
//
// NVIDIA Developer Blog
// https://devblogs.nvidia.com/how-query-device-properties-and-handle-errors-cuda-cc/
// https://devblogs.nvidia.com/cuda-pro-tip-the-fast-way-to-query-device-properties/
//
// Stack Overflow
// https://stackoverflow.com/questions/22520209/programmatically-retrieve-maximum-number-of-blocks-per-multiprocessor
// ***************************************************************************
// This sequential program queries device properties.
//
// For Turing, use the script on the same directory to compile and run.
//
// For TACC Maverick 2, use the script to run this code:
// sbatch YujinYoshimura.script
//
// Otherwise, use TACC Maverick 2 command to compile:
// nvcc YujinYoshimura.cu -o YujinYoshimura_Exe
// TACC Maverick 2 command to run executable:
// ./YujinYoshimura_Exe
// ***************************************************************************

#include <cuda.h>
#include <stdio.h>

// ***************************************************************************
// Function Name: main
// Parameters: int, char**
// Return: int
// Description: Main function of the program.
// ***************************************************************************
int main(int argc, char **argv) {
  int nDevices, blocks, version;

  cudaGetDeviceCount(&nDevices);
  for (int i = 0; i < nDevices; i++) {
    cudaDeviceProp prop;
    cudaGetDeviceProperties(&prop, i);
    cudaDeviceGetAttribute(&version, cudaDevAttrComputeCapabilityMajor, i);
    // According to Feature Support per Compute Capability
    if (version < 3) {
      blocks = 8;
    } else if (version < 5) {
      blocks = 16;
    } else {
      blocks = 32;
    }
    printf("=============================================================\n");
    printf("Device Number: %d\n", i);
    printf("  Device name: %s\n", prop.name);
    printf("  Size of shared memory per block: %zu\n", prop.sharedMemPerBlock);
    printf("  Number of registers per block: %d\n", prop.regsPerBlock);
    printf("  The corresponding warp size: %d\n", prop.warpSize);
    printf("  The maximum number of threads per block: %d\n", prop.maxThreadsPerBlock);
    printf("  The maximum number of threads that we can have\n");
    printf("    for a 3D layout: (%d, %d, %d)\n", prop.maxThreadsDim[0], prop.maxThreadsDim[1], prop.maxThreadsDim[2]);
    printf("  The maximum grid size: (%d, %d, %d)\n", prop.maxGridSize[0], prop.maxGridSize[1], prop.maxGridSize[2]);
    printf("  Max number of blocks per streaming multiprocessor: %d \n", blocks);
  }
  printf("=============================================================\n");

 	return EXIT_SUCCESS;
}

	.file	"tmpxft_0030ea57_00000000-6_YujinYoshimura.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2030:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2030:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"=============================================================\n"
.LC1:
	.string	"Device Number: %d\n"
.LC2:
	.string	"  Device name: %s\n"
.LC3:
	.string	"  Size of shared memory per block: %zu\n"
.LC4:
	.string	"  Number of registers per block: %d\n"
.LC5:
	.string	"  The corresponding warp size: %d\n"
.LC6:
	.string	"  The maximum number of threads per block: %d\n"
.LC7:
	.string	"  The maximum number of threads that we can have\n"
.LC8:
	.string	"    for a 3D layout: (%d, %d, %d)\n"
.LC9:
	.string	"  The maximum grid size: (%d, %d, %d)\n"
.LC10:
	.string	"  Max number of blocks per streaming multiprocessor: %d \n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2027:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	leaq	.LC0(%rip), %r12
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	xorl	%ebx, %ebx
	subq	$1064, %rsp
	.cfi_def_cfa_offset 1104
	movq	%fs:40, %rax
	movq	%rax, 1048(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %rdi
	call	cudaGetDeviceCount@PLT
.L3:
	cmpl	%ebx, 8(%rsp)
	jle	.L11
	leaq	16(%rsp), %r13
	movl	%ebx, %esi
	movl	$8, %ebp
	movq	%r13, %rdi
	call	cudaGetDeviceProperties_v2@PLT
	leaq	12(%rsp), %rdi
	movl	%ebx, %edx
	movl	$75, %esi
	call	cudaDeviceGetAttribute@PLT
	movl	12(%rsp), %eax
	cmpl	$2, %eax
	jle	.L4
	cmpl	$4, %eax
	movl	$32, %ebp
	movl	$16, %eax
	cmovle	%eax, %ebp
.L4:
	movq	%r12, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	%ebx, %edx
	leaq	.LC1(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	incl	%ebx
	call	__printf_chk@PLT
	movq	%r13, %rdx
	movl	$2, %edi
	xorl	%eax, %eax
	leaq	.LC2(%rip), %rsi
	call	__printf_chk@PLT
	movq	312(%rsp), %rdx
	leaq	.LC3(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	320(%rsp), %edx
	leaq	.LC4(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	324(%rsp), %edx
	leaq	.LC5(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	336(%rsp), %edx
	leaq	.LC6(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	leaq	.LC7(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	348(%rsp), %r8d
	xorl	%eax, %eax
	movl	344(%rsp), %ecx
	movl	340(%rsp), %edx
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	360(%rsp), %r8d
	xorl	%eax, %eax
	movl	356(%rsp), %ecx
	movl	352(%rsp), %edx
	leaq	.LC9(%rip), %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movl	%ebp, %edx
	leaq	.LC10(%rip), %rsi
	xorl	%eax, %eax
	movl	$2, %edi
	call	__printf_chk@PLT
	jmp	.L3
.L11:
	xorl	%eax, %eax
	movq	%r12, %rsi
	movl	$2, %edi
	call	__printf_chk@PLT
	movq	1048(%rsp), %rax
	subq	%fs:40, %rax
	je	.L6
	call	__stack_chk_fail@PLT
.L6:
	addq	$1064, %rsp
	.cfi_def_cfa_offset 40
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2027:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2053:
	.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
.LFE2053:
	.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
	.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:

// ***************************************************************************
// Assignment #3
// Name: Yujin Yoshimura
// Parallel Programming Date: March 12, 2020
// ***************************************************************************
// References:
//
// NVIDIA CUDA Toolkit Documentation
// https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__DEVICE.html
// https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#features-and-technical-specifications
//
// NVIDIA Developer Blog
// https://devblogs.nvidia.com/how-query-device-properties-and-handle-errors-cuda-cc/
// https://devblogs.nvidia.com/cuda-pro-tip-the-fast-way-to-query-device-properties/
//
// Stack Overflow
// https://stackoverflow.com/questions/22520209/programmatically-retrieve-maximum-number-of-blocks-per-multiprocessor
// ***************************************************************************
// This sequential program queries device properties.
//
// For Turing, use the script on the same directory to compile and run.
//
// For TACC Maverick 2, use the script to run this code:
// sbatch YujinYoshimura.script
//
// Otherwise, use TACC Maverick 2 command to compile:
// nvcc YujinYoshimura.cu -o YujinYoshimura_Exe
// TACC Maverick 2 command to run executable:
// ./YujinYoshimura_Exe
// ***************************************************************************

#include <hip/hip_runtime.h>
#include <stdio.h>

// ***************************************************************************
// Function Name: main
// Parameters: int, char**
// Return: int
// Description: Main function of the program.
// ***************************************************************************
int main(int argc, char **argv) {
  int nDevices, blocks, version;

  hipGetDeviceCount(&nDevices);
  for (int i = 0; i < nDevices; i++) {
    hipDeviceProp_t prop;
    hipGetDeviceProperties(&prop, i);
    hipDeviceGetAttribute(&version, hipDeviceAttributeComputeCapabilityMajor, i);
    // According to Feature Support per Compute Capability
    if (version < 3) {
      blocks = 8;
    } else if (version < 5) {
      blocks = 16;
    } else {
      blocks = 32;
    }
    printf("=============================================================\n");
    printf("Device Number: %d\n", i);
    printf("  Device name: %s\n", prop.name);
    printf("  Size of shared memory per block: %zu\n", prop.sharedMemPerBlock);
    printf("  Number of registers per block: %d\n", prop.regsPerBlock);
    printf("  The corresponding warp size: %d\n", prop.warpSize);
    printf("  The maximum number of threads per block: %d\n", prop.maxThreadsPerBlock);
    printf("  The maximum number of threads that we can have\n");
    printf("    for a 3D layout: (%d, %d, %d)\n", prop.maxThreadsDim[0], prop.maxThreadsDim[1], prop.maxThreadsDim[2]);
    printf("  The maximum grid size: (%d, %d, %d)\n", prop.maxGridSize[0], prop.maxGridSize[1], prop.maxGridSize[2]);
    printf("  Max number of blocks per streaming multiprocessor: %d \n", blocks);
  }
  printf("=============================================================\n");

 	return EXIT_SUCCESS;
}

	.text
	.file	"YujinYoshimura.hip"
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$1488, %rsp                     # imm = 0x5D0
	.cfi_def_cfa_offset 1536
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	leaq	12(%rsp), %rbx
	movq	%rbx, %rdi
	callq	hipGetDeviceCount
	cmpl	$0, (%rbx)
	jle	.LBB0_3
# %bb.1:                                # %.lr.ph
	leaq	16(%rsp), %rbx
	leaq	8(%rsp), %r14
	movl	$8, %r12d
	xorl	%ebp, %ebp
.LBB0_2:                                # =>This Inner Loop Header: Depth=1
	movq	%rbx, %rdi
	movl	%ebp, %esi
	callq	hipGetDevicePropertiesR0600
	movq	%r14, %rdi
	movl	$23, %esi
	movl	%ebp, %edx
	callq	hipDeviceGetAttribute
	movl	8(%rsp), %eax
	xorl	%r15d, %r15d
	cmpl	$5, %eax
	setge	%r15b
	shll	$4, %r15d
	addl	$16, %r15d
	cmpl	$3, %eax
	cmovll	%r12d, %r15d
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	movl	$.L.str.1, %edi
	movl	%ebp, %esi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.2, %edi
	movq	%rbx, %rsi
	xorl	%eax, %eax
	callq	printf
	movq	312(%rsp), %rsi
	movl	$.L.str.3, %edi
	xorl	%eax, %eax
	callq	printf
	movl	320(%rsp), %esi
	movl	$.L.str.4, %edi
	xorl	%eax, %eax
	callq	printf
	movl	324(%rsp), %esi
	movl	$.L.str.5, %edi
	xorl	%eax, %eax
	callq	printf
	movl	336(%rsp), %esi
	movl	$.L.str.6, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$.Lstr.2, %edi
	callq	puts@PLT
	movl	340(%rsp), %esi
	movl	344(%rsp), %edx
	movl	348(%rsp), %ecx
	movl	$.L.str.8, %edi
	xorl	%eax, %eax
	callq	printf
	movl	352(%rsp), %esi
	movl	356(%rsp), %edx
	movl	360(%rsp), %ecx
	movl	$.L.str.9, %edi
	xorl	%eax, %eax
	callq	printf
	movl	$.L.str.10, %edi
	movl	%r15d, %esi
	xorl	%eax, %eax
	callq	printf
	incl	%ebp
	cmpl	12(%rsp), %ebp
	jl	.LBB0_2
.LBB0_3:                                # %._crit_edge
	movl	$.Lstr.1, %edi
	callq	puts@PLT
	xorl	%eax, %eax
	addq	$1488, %rsp                     # imm = 0x5D0
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str.1,@object                # @.str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.1:
	.asciz	"Device Number: %d\n"
	.size	.L.str.1, 19

	.type	.L.str.2,@object                # @.str.2
.L.str.2:
	.asciz	"  Device name: %s\n"
	.size	.L.str.2, 19

	.type	.L.str.3,@object                # @.str.3
.L.str.3:
	.asciz	"  Size of shared memory per block: %zu\n"
	.size	.L.str.3, 40

	.type	.L.str.4,@object                # @.str.4
.L.str.4:
	.asciz	"  Number of registers per block: %d\n"
	.size	.L.str.4, 37

	.type	.L.str.5,@object                # @.str.5
.L.str.5:
	.asciz	"  The corresponding warp size: %d\n"
	.size	.L.str.5, 35

	.type	.L.str.6,@object                # @.str.6
.L.str.6:
	.asciz	"  The maximum number of threads per block: %d\n"
	.size	.L.str.6, 47

	.type	.L.str.8,@object                # @.str.8
.L.str.8:
	.asciz	"    for a 3D layout: (%d, %d, %d)\n"
	.size	.L.str.8, 35

	.type	.L.str.9,@object                # @.str.9
.L.str.9:
	.asciz	"  The maximum grid size: (%d, %d, %d)\n"
	.size	.L.str.9, 39

	.type	.L.str.10,@object               # @.str.10
.L.str.10:
	.asciz	"  Max number of blocks per streaming multiprocessor: %d \n"
	.size	.L.str.10, 58

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

	.type	.Lstr.1,@object                 # @str.1
	.section	.rodata.str1.1,"aMS",@progbits,1
.Lstr.1:
	.asciz	"============================================================="
	.size	.Lstr.1, 62

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"  The maximum number of threads that we can have"
	.size	.Lstr.2, 49

	.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
