
#include <stdio.h>
#include "cuda_runtime.h"

int gridSize(int nElem, int blockSz) {
  return (nElem + blockSz - 1)/blockSz;
}

void gridResize(dim3 block, dim3 grid, int nElem, int blockSz){
  block.x = blockSz;
  grid.x = gridSize(nElem, block.x);
  printf("Blocks: %d, grids: %d\n", block.x, grid.x);
}

int main() {
  //define total data elements
  int nElem = 1024;
  
  //inite block and grid sizes
  dim3 block(1024);
  dim3 grid (gridSize(nElem, block.x));
  printf("Initial block size: %d, grid size: %d\n", block.x, grid.x);
  
  //variate block sizes
  for (int i = nElem; i>1; i/=2)
    gridResize(block, grid, nElem, i);
  
  cudaDeviceSynchronize();
  cudaDeviceReset();
  
  return 0;
}

	.file	"tmpxft_002d4273_00000000-6_defineGridBlock.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2032:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2032:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z8gridSizeii
	.type	_Z8gridSizeii, @function
_Z8gridSizeii:
.LFB2027:
	.cfi_startproc
	endbr64
	leal	-1(%rsi,%rdi), %eax
	cltd
	idivl	%esi
	ret
	.cfi_endproc
.LFE2027:
	.size	_Z8gridSizeii, .-_Z8gridSizeii
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Blocks: %d, grids: %d\n"
	.text
	.globl	_Z10gridResize4dim3S_ii
	.type	_Z10gridResize4dim3S_ii, @function
_Z10gridResize4dim3S_ii:
.LFB2028:
	.cfi_startproc
	endbr64
	leal	-1(%r9,%r8), %eax
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	cltd
	idivl	%r9d
	movl	%r9d, %edx
	movl	%eax, %ecx
	xorl	%eax, %eax
	jmp	__printf_chk@PLT
	.cfi_endproc
.LFE2028:
	.size	_Z10gridResize4dim3S_ii, .-_Z10gridResize4dim3S_ii
	.section	.rodata.str1.1
.LC1:
	.string	"Initial block size: %d, grid size: %d\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2029:
	.cfi_startproc
	endbr64
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movl	$1, %ecx
	movl	$1024, %edx
	movl	$2, %edi
	pushq	%rbx
	.cfi_def_cfa_offset 24
	.cfi_offset 3, -24
	movl	$1024, %ebp
	movl	$10, %ebx
	movabsq	$4294967297, %rax
	leaq	.LC1(%rip), %rsi
	subq	$40, %rsp
	.cfi_def_cfa_offset 64
	movq	%rax, 12(%rsp)
	xorl	%eax, %eax
	movl	$1, 24(%rsp)
	movl	$1, 28(%rsp)
	call	__printf_chk@PLT
.L5:
	movl	$1024, 8(%rsp)
	movl	28(%rsp), %ecx
	movl	%ebp, %r9d
	movl	$1024, %r8d
	movl	$1, 20(%rsp)
	movq	8(%rsp), %rdi
	sarl	%ebp
	movq	20(%rsp), %rdx
	movl	16(%rsp), %esi
	call	_Z10gridResize4dim3S_ii
	decl	%ebx
	jne	.L5
	call	cudaDeviceSynchronize@PLT
	call	cudaDeviceReset@PLT
	addq	$40, %rsp
	.cfi_def_cfa_offset 24
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2029:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2055:
	.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
.LFE2055:
	.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:


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

int gridSize(int nElem, int blockSz) {
  return (nElem + blockSz - 1)/blockSz;
}

void gridResize(dim3 block, dim3 grid, int nElem, int blockSz){
  block.x = blockSz;
  grid.x = gridSize(nElem, block.x);
  printf("Blocks: %d, grids: %d\n", block.x, grid.x);
}

int main() {
  //define total data elements
  int nElem = 1024;
  
  //inite block and grid sizes
  dim3 block(1024);
  dim3 grid (gridSize(nElem, block.x));
  printf("Initial block size: %d, grid size: %d\n", block.x, grid.x);
  
  //variate block sizes
  for (int i = nElem; i>1; i/=2)
    gridResize(block, grid, nElem, i);
  
  hipDeviceSynchronize();
  hipDeviceReset();
  
  return 0;
}

	.text
	.file	"defineGridBlock.hip"
	.globl	_Z8gridSizeii                   # -- Begin function _Z8gridSizeii
	.type	_Z8gridSizeii,@function
_Z8gridSizeii:                          # @_Z8gridSizeii
	.cfi_startproc
# %bb.0:
                                        # kill: def $esi killed $esi def $rsi
                                        # kill: def $edi killed $edi def $rdi
	leal	(%rdi,%rsi), %eax
	decl	%eax
	cltd
	idivl	%esi
	retq
.Lfunc_end0:
	.size	_Z8gridSizeii, .Lfunc_end0-_Z8gridSizeii
	.cfi_endproc
                                        # -- End function
	.globl	_Z10gridResize4dim3S_ii         # -- Begin function _Z10gridResize4dim3S_ii
	.type	_Z10gridResize4dim3S_ii,@function
_Z10gridResize4dim3S_ii:                # @_Z10gridResize4dim3S_ii
	.cfi_startproc
# %bb.0:
	movl	%r9d, %esi
                                        # kill: def $r8d killed $r8d def $r8
	leal	(%r8,%rsi), %eax
	decl	%eax
	cltd
	idivl	%r9d
	movl	$.L.str, %edi
                                        # kill: def $esi killed $esi killed $rsi
	movl	%eax, %edx
	xorl	%eax, %eax
	jmp	printf                          # TAILCALL
.Lfunc_end1:
	.size	_Z10gridResize4dim3S_ii, .Lfunc_end1-_Z10gridResize4dim3S_ii
	.cfi_endproc
                                        # -- End function
	.globl	main                            # -- Begin function main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset %rbx, -16
	movl	$1024, %ebx                     # imm = 0x400
	movl	$.L.str.1, %edi
	movl	$1024, %esi                     # imm = 0x400
	movl	$1, %edx
	xorl	%eax, %eax
	callq	printf
.LBB2_1:                                # =>This Inner Loop Header: Depth=1
	leal	1023(%rbx), %eax
	xorl	%edx, %edx
	divl	%ebx
	movl	$.L.str, %edi
	movl	%ebx, %esi
	movl	%eax, %edx
	xorl	%eax, %eax
	callq	printf
	movl	%ebx, %eax
	shrl	%eax
	cmpl	$3, %ebx
	movl	%eax, %ebx
	ja	.LBB2_1
# %bb.2:
	callq	hipDeviceSynchronize
	callq	hipDeviceReset
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	main, .Lfunc_end2-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Blocks: %d, grids: %d\n"
	.size	.L.str, 23

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Initial block size: %d, grid size: %d\n"
	.size	.L.str.1, 39

	.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
