/************************************************
FILENAME: example_paddedpencil.cu

AUTHOR: Anuva K

DESCRIPTION: Test code to perform 3d FFTs on CUDA
according to the proposed pruned framework. FFTs of a small
non-zero subvolume of a larger volume of zeros are to be 
computed pencil by pencil without storing the large 3d array
This script tests X dimension FFT of k x k x k signal to N x k x k
*/ 



#include <cufft.h>
#include <cuComplex.h>
#include <stdio.h>
#define N_SIGS 1 
#define IN_SIG_LEN 8
#define OUT_SIG_LEN 8
int main(){

  cuFloatComplex *h_signal, *d_signal, *h_result, *d_result;

  h_signal = (cuFloatComplex *)malloc(N_SIGS*IN_SIG_LEN*sizeof(cuFloatComplex));
  h_result = (cuFloatComplex *)malloc(N_SIGS*OUT_SIG_LEN*sizeof(cuFloatComplex));
  for (int i = 0; i < N_SIGS; i ++)
    for (int j = 0; j < IN_SIG_LEN/2; j++) // to include padding
  h_signal[(i*IN_SIG_LEN) + j] = make_cuFloatComplex(100*sin((i+1)*6.283*j/IN_SIG_LEN), 0); //this is how to put data into cuFloatComplex type variable
  cudaMalloc(&d_signal, N_SIGS*IN_SIG_LEN*sizeof(cuFloatComplex));
  cudaMalloc(&d_result, N_SIGS*OUT_SIG_LEN*sizeof(cuFloatComplex));

  cudaMemcpy(d_signal, h_signal, N_SIGS*IN_SIG_LEN*sizeof(cuFloatComplex), cudaMemcpyHostToDevice);
  cufftHandle plan;
  int n[1] = {IN_SIG_LEN};

  cufftResult res = cufftPlanMany(&plan, 1, n,
     NULL, 1, IN_SIG_LEN,  //advanced data layout, NULL shuts it off. idist=IN_SIG_LEN
     NULL, 1, OUT_SIG_LEN,  //advanced data layout, NULL shuts it off. odist= OUT_SIG_LEN
     CUFFT_C2C, N_SIGS);
  if (res != CUFFT_SUCCESS) {printf("plan create fail\n"); return 1;}

  res = cufftExecC2C(plan, d_signal, d_result, CUFFT_FORWARD);
  if (res != CUFFT_SUCCESS) {printf("forward transform fail\n"); return 1;}
  cudaMemcpy(h_result, d_result, N_SIGS*OUT_SIG_LEN*sizeof(cuFloatComplex), cudaMemcpyDeviceToHost);

  for (int i = 0; i < N_SIGS; i++){
    for (int j = 0; j < IN_SIG_LEN; j++)
      printf("%.3f ", cuCrealf(h_signal[(i*IN_SIG_LEN)+j]));
    printf("\n"); }

  printf("result:\n");


  for (int i = 0; i < N_SIGS; i++){
    for (int j = 0; j < OUT_SIG_LEN; j++)
      printf("%.3f ", cuCrealf(h_result[(i*OUT_SIG_LEN)+j]));
    printf("\n"); }

  return 0;
}

	.file	"tmpxft_00386a6e_00000000-6_example_paddedpencil.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2053:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2053:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC4:
	.string	"plan create fail\n"
.LC5:
	.string	"forward transform fail\n"
.LC6:
	.string	"%.3f "
.LC7:
	.string	"\n"
.LC8:
	.string	"result:\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2050:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	movl	$64, %edi
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	xorl	%ebp, %ebp
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	subq	$56, %rsp
	.cfi_def_cfa_offset 96
	movq	%fs:40, %rax
	movq	%rax, 40(%rsp)
	xorl	%eax, %eax
	call	malloc@PLT
	movl	$64, %edi
	movq	%rax, %rbx
	call	malloc@PLT
	movq	%rax, %r12
.L3:
	cvtsi2sdl	%ebp, %xmm0
	mulsd	.LC0(%rip), %xmm0
	mulsd	.LC1(%rip), %xmm0
	call	sin@PLT
	mulsd	.LC2(%rip), %xmm0
	movl	$0x00000000, 4(%rbx,%rbp,8)
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, (%rbx,%rbp,8)
	incq	%rbp
	cmpq	$4, %rbp
	jne	.L3
	leaq	16(%rsp), %rdi
	movl	$64, %esi
	call	cudaMalloc@PLT
	leaq	24(%rsp), %rdi
	movl	$64, %esi
	call	cudaMalloc@PLT
	movq	16(%rsp), %rdi
	movl	$1, %ecx
	movq	%rbx, %rsi
	movl	$64, %edx
	call	cudaMemcpy@PLT
	movl	$8, 36(%rsp)
	leaq	36(%rsp), %rdx
	leaq	12(%rsp), %rdi
	pushq	%rax
	.cfi_def_cfa_offset 104
	movl	$1, %esi
	movl	$8, %r9d
	xorl	%ecx, %ecx
	pushq	$1
	.cfi_def_cfa_offset 112
	movl	$1, %r8d
	pushq	$41
	.cfi_def_cfa_offset 120
	pushq	$8
	.cfi_def_cfa_offset 128
	pushq	$1
	.cfi_def_cfa_offset 136
	pushq	$0
	.cfi_def_cfa_offset 144
	call	cufftPlanMany@PLT
	addq	$48, %rsp
	.cfi_def_cfa_offset 96
	leaq	.LC4(%rip), %rsi
	testl	%eax, %eax
	jne	.L15
	movq	24(%rsp), %rdx
	movq	16(%rsp), %rsi
	orl	$-1, %ecx
	movl	12(%rsp), %edi
	call	cufftExecC2C@PLT
	testl	%eax, %eax
	je	.L6
	leaq	.LC5(%rip), %rsi
.L15:
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	movl	$1, %eax
	jmp	.L2
.L6:
	movq	24(%rsp), %rsi
	movl	$2, %ecx
	movq	%r12, %rdi
	xorl	%ebp, %ebp
	movl	$64, %edx
	leaq	.LC6(%rip), %r13
	call	cudaMemcpy@PLT
.L8:
	cvtss2sd	(%rbx,%rbp,8), %xmm0
	movq	%r13, %rsi
	movl	$2, %edi
	movb	$1, %al
	call	__printf_chk@PLT
	incq	%rbp
	cmpq	$8, %rbp
	jne	.L8
	leaq	.LC7(%rip), %rbp
	movl	$2, %edi
	xorl	%eax, %eax
	xorl	%ebx, %ebx
	movq	%rbp, %rsi
	call	__printf_chk@PLT
	leaq	.LC8(%rip), %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
.L9:
	cvtss2sd	(%r12,%rbx,8), %xmm0
	movq	%r13, %rsi
	movl	$2, %edi
	movb	$1, %al
	call	__printf_chk@PLT
	incq	%rbx
	cmpq	$8, %rbx
	jne	.L9
	movq	%rbp, %rsi
	movl	$2, %edi
	xorl	%eax, %eax
	call	__printf_chk@PLT
	xorl	%eax, %eax
.L2:
	movq	40(%rsp), %rdx
	subq	%fs:40, %rdx
	je	.L10
	call	__stack_chk_fail@PLT
.L10:
	addq	$56, %rsp
	.cfi_def_cfa_offset 40
	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
.LFE2050:
	.size	main, .-main
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2076:
	.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
.LFE2076:
	.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
.LC0:
	.long	-1065151889
	.long	1075388874
	.align 8
.LC1:
	.long	0
	.long	1069547520
	.align 8
.LC2:
	.long	0
	.long	1079574528
	.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:

/************************************************
FILENAME: example_paddedpencil.cu

AUTHOR: Anuva K

DESCRIPTION: Test code to perform 3d FFTs on CUDA
according to the proposed pruned framework. FFTs of a small
non-zero subvolume of a larger volume of zeros are to be 
computed pencil by pencil without storing the large 3d array
This script tests X dimension FFT of k x k x k signal to N x k x k
*/ 




#include <hip/hip_runtime.h>
#include <hipfft/hipfft.h>
#include <hip/hip_complex.h>
#include <stdio.h>
#define N_SIGS 1 
#define IN_SIG_LEN 8
#define OUT_SIG_LEN 8
int main(){

  hipFloatComplex *h_signal, *d_signal, *h_result, *d_result;

  h_signal = (hipFloatComplex *)malloc(N_SIGS*IN_SIG_LEN*sizeof(hipFloatComplex));
  h_result = (hipFloatComplex *)malloc(N_SIGS*OUT_SIG_LEN*sizeof(hipFloatComplex));
  for (int i = 0; i < N_SIGS; i ++)
    for (int j = 0; j < IN_SIG_LEN/2; j++) // to include padding
  h_signal[(i*IN_SIG_LEN) + j] = make_hipFloatComplex(100*sin((i+1)*6.283*j/IN_SIG_LEN), 0); //this is how to put data into cuFloatComplex type variable
  hipMalloc(&d_signal, N_SIGS*IN_SIG_LEN*sizeof(hipFloatComplex));
  hipMalloc(&d_result, N_SIGS*OUT_SIG_LEN*sizeof(hipFloatComplex));

  hipMemcpy(d_signal, h_signal, N_SIGS*IN_SIG_LEN*sizeof(hipFloatComplex), hipMemcpyHostToDevice);
  hipfftHandle plan;
  int n[1] = {IN_SIG_LEN};

  hipfftResult res = hipfftPlanMany(&plan, 1, n,
     NULL, 1, IN_SIG_LEN,  //advanced data layout, NULL shuts it off. idist=IN_SIG_LEN
     NULL, 1, OUT_SIG_LEN,  //advanced data layout, NULL shuts it off. odist= OUT_SIG_LEN
     HIPFFT_C2C, N_SIGS);
  if (res != HIPFFT_SUCCESS) {printf("plan create fail\n"); return 1;}

  res = hipfftExecC2C(plan, d_signal, d_result, HIPFFT_FORWARD);
  if (res != HIPFFT_SUCCESS) {printf("forward transform fail\n"); return 1;}
  hipMemcpy(h_result, d_result, N_SIGS*OUT_SIG_LEN*sizeof(hipFloatComplex), hipMemcpyDeviceToHost);

  for (int i = 0; i < N_SIGS; i++){
    for (int j = 0; j < IN_SIG_LEN; j++)
      printf("%.3f ", hipCrealf(h_signal[(i*IN_SIG_LEN)+j]));
    printf("\n"); }

  printf("result:\n");


  for (int i = 0; i < N_SIGS; i++){
    for (int j = 0; j < OUT_SIG_LEN; j++)
      printf("%.3f ", hipCrealf(h_result[(i*OUT_SIG_LEN)+j]));
    printf("\n"); }

  return 0;
}

	.text
	.file	"example_paddedpencil.hip"
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function main
.LCPI0_0:
	.quad	0x401921cac083126f              # double 6.2830000000000004
.LCPI0_1:
	.quad	0x3fc0000000000000              # double 0.125
.LCPI0_2:
	.quad	0x4059000000000000              # double 100
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:                                # %.critedge
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%rbx
	.cfi_def_cfa_offset 32
	subq	$32, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	movl	$64, %edi
	callq	malloc
	movq	%rax, %r14
	movl	$64, %edi
	callq	malloc
	movq	%rax, %rbx
	xorl	%r15d, %r15d
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
	xorps	%xmm0, %xmm0
	cvtsi2sd	%r15d, %xmm0
	mulsd	.LCPI0_0(%rip), %xmm0
	mulsd	.LCPI0_1(%rip), %xmm0
	callq	sin
	mulsd	.LCPI0_2(%rip), %xmm0
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, (%rsp)
	movl	$0, 4(%rsp)
	movsd	(%rsp), %xmm0                   # xmm0 = mem[0],zero
	movsd	%xmm0, (%r14,%r15,8)
	incq	%r15
	cmpq	$4, %r15
	jne	.LBB0_1
# %bb.2:
	movq	%rsp, %r15
	movl	$64, %esi
	movq	%r15, %rdi
	callq	hipMalloc
	leaq	8(%rsp), %rdi
	movl	$64, %esi
	callq	hipMalloc
	movq	(%r15), %rdi
	movl	$1, %r15d
	movl	$64, %edx
	movq	%r14, %rsi
	movl	$1, %ecx
	callq	hipMemcpy
	movl	$8, %eax
	leaq	28(%rsp), %rdx
	movl	%eax, (%rdx)
	subq	$8, %rsp
	.cfi_adjust_cfa_offset 8
	leaq	24(%rsp), %rdi
	movl	$1, %esi
	xorl	%ecx, %ecx
	movl	$1, %r8d
	movl	$8, %r9d
	pushq	%r15
	.cfi_adjust_cfa_offset 8
	pushq	$41
	.cfi_adjust_cfa_offset 8
	pushq	%rax
	.cfi_adjust_cfa_offset 8
	pushq	%r15
	.cfi_adjust_cfa_offset 8
	pushq	$0
	.cfi_adjust_cfa_offset 8
	callq	hipfftPlanMany
	addq	$48, %rsp
	.cfi_adjust_cfa_offset -48
	testl	%eax, %eax
	je	.LBB0_5
# %bb.3:
	movl	$.Lstr.2, %edi
	jmp	.LBB0_4
.LBB0_5:
	movq	16(%rsp), %rdi
	movq	(%rsp), %rsi
	movq	8(%rsp), %rdx
	movl	$-1, %ecx
	callq	hipfftExecC2C
	testl	%eax, %eax
	je	.LBB0_7
# %bb.6:
	movl	$.Lstr.1, %edi
.LBB0_4:
	callq	puts@PLT
.LBB0_12:
	movl	%r15d, %eax
	addq	$32, %rsp
	.cfi_def_cfa_offset 32
	popq	%rbx
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.LBB0_7:                                # %.critedge40
	.cfi_def_cfa_offset 64
	movq	8(%rsp), %rsi
	movl	$64, %edx
	movq	%rbx, %rdi
	movl	$2, %ecx
	callq	hipMemcpy
	xorl	%r15d, %r15d
.LBB0_8:                                # =>This Inner Loop Header: Depth=1
	xorps	%xmm0, %xmm0
	cvtss2sd	(%r14,%r15,8), %xmm0
	movl	$.L.str.2, %edi
	movb	$1, %al
	callq	printf
	incq	%r15
	cmpq	$8, %r15
	jne	.LBB0_8
# %bb.9:                                # %.critedge42
	movl	$10, %edi
	callq	putchar@PLT
	movl	$.Lstr, %edi
	callq	puts@PLT
	xorl	%r14d, %r14d
.LBB0_10:                               # =>This Inner Loop Header: Depth=1
	xorps	%xmm0, %xmm0
	cvtss2sd	(%rbx,%r14,8), %xmm0
	movl	$.L.str.2, %edi
	movb	$1, %al
	callq	printf
	incq	%r14
	cmpq	$8, %r14
	jne	.LBB0_10
# %bb.11:
	movl	$10, %edi
	callq	putchar@PLT
	xorl	%r15d, %r15d
	jmp	.LBB0_12
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc
                                        # -- End function
	.type	.L.str.2,@object                # @.str.2
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str.2:
	.asciz	"%.3f "
	.size	.L.str.2, 6

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

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

	.type	.Lstr.1,@object                 # @str.1
.Lstr.1:
	.asciz	"forward transform fail"
	.size	.Lstr.1, 23

	.type	.Lstr.2,@object                 # @str.2
.Lstr.2:
	.asciz	"plan create fail"
	.size	.Lstr.2, 17

	.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
