// File: /workspace/github_c_makefile_star2_O2/USArmyResearchLab[P]ARL_OpenSHMEM/test/reduce.c
// Function: main
int main (void)
{
	int i, nelement;
	static unsigned int t, tsum;
	static long pSync[SHMEM_REDUCE_SYNC_SIZE];
	for (i = 0; i < SHMEM_REDUCE_SYNC_SIZE; i++) {
		pSync[i] = SHMEM_SYNC_VALUE;
	}

	shmem_init();
	int me = shmem_my_pe();
	int npes = shmem_n_pes();

	int pwrk_elems = NELEMENT/2 + 1;
	pwrk_elems = (pwrk_elems > SHMEM_REDUCE_MIN_WRKDATA_SIZE) ? 
		pwrk_elems : SHMEM_REDUCE_MIN_WRKDATA_SIZE;

	int* source = (int*)shmem_malloc(NELEMENT * sizeof (int) * 1000);
	int* target = (int*)shmem_malloc(NELEMENT * sizeof (int) * 1000);
	int* pwrk   = (int*)shmem_malloc(pwrk_elems * sizeof(int) * 1000);
	for (i = 0; i < NELEMENT; i++) {
		source[i] = i;
	}

	if (me == 0) {
		printf("# SHMEM Reduction times for NPES = %d\n" \
			"# Nelements \tLatency (nanoseconds)\n", npes);
	}

	for (nelement = 1; nelement <= NELEMENT; nelement <<= 1) {

		// reset values for each iteration
		for (i = 0; i < NELEMENT; i++) {
			target[i] = -90;
		}
		shmem_barrier_all();
		ctimer_start();

		t = ctimer();
		for (i = 0; i < NLOOP; i++) {
			shmem_int_sum_to_all(target,source,nelement,0,0,npes, pwrk, pSync);
		}
		t -= ctimer();

		shmem_int_sum_to_all(&tsum, &t, 1, 0, 0, npes, pwrk, pSync);

		if (me == 0) {
			unsigned int nsec = ctimer_nsec(tsum / (npes * NLOOP));
			printf("%5d %7u\n", nelement, nsec);
		}

		int err = 0;
		for (i = 0; i < nelement; i++) {
			if (target[i] != i*npes) {
				printf("%d# error %d %d %d\n",me,i,target[i],i*npes);
				err++;
			}
		}
		if (err) printf("# %d: ERRORS %d\n", me, err);
	}

	shmem_free(target);
	shmem_free(source);

	shmem_finalize();

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
xorl %eax, %eax
leaq 0x4e0a(%rip), %r14      # 0x6170
cmpq $0x50, %rax
je 0x1377
andq $0x0, (%rax,%r14)
addq $0x8, %rax
jmp 0x1366
callq 0x162c
leaq 0x4db5(%rip), %rax      # 0x6138
movl (%rax), %eax
movl %eax, -0x30(%rbp)
leaq 0x4dad(%rip), %rax      # 0x613c
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
movl $0x7d0000, %edi         # imm = 0x7D0000
callq 0x1c28
movq %rax, %r15
movl $0x7d0000, %edi         # imm = 0x7D0000
callq 0x1c28
movq %rax, %r12
movl $0x3e8fa0, %edi         # imm = 0x3E8FA0
callq 0x1c28
movq %rax, %r13
xorl %eax, %eax
cmpq $0x800, %rax            # imm = 0x800
je 0x13ce
movl %eax, (%r15,%rax,4)
incq %rax
jmp 0x13bd
cmpl $0x0, -0x30(%rbp)
jne 0x13e5
leaq 0x2c45(%rip), %rdi      # 0x4020
movl -0x2c(%rbp), %esi
xorl %eax, %eax
callq 0x10e0
imull $0xa, -0x2c(%rbp), %eax
movl %eax, -0x38(%rbp)
pushq $0x1
popq %rcx
movq %r15, -0x58(%rbp)
movq %r13, -0x50(%rbp)
cmpl $0x800, %ecx            # imm = 0x800
ja 0x15e1
xorl %eax, %eax
cmpq $0x800, %rax            # imm = 0x800
je 0x141a
movl $0xffffffa6, (%r12,%rax,4) # imm = 0xFFFFFFA6
incq %rax
jmp 0x1405
movl %ecx, -0x34(%rbp)
callq 0x1608
leaq 0x4d97(%rip), %rdi      # 0x61c0
xorl %esi, %esi
callq 0x1110
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
callq 0x1110
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
subq 0x4d76(%rip), %rax      # 0x61c0
cvtsi2sd %rax, %xmm0
subq 0x4d72(%rip), %rcx      # 0x61c8
cvtsi2sd %rcx, %xmm1
mulsd 0x2ba5(%rip), %xmm1     # 0x4008
mulsd 0x2ba5(%rip), %xmm0     # 0x4010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x2b95(%rip), %xmm0     # 0x4018
cvttsd2si %xmm0, %rdx
andl %ecx, %edx
orl %edx, %eax
notl %eax
movl %eax, 0x4ccc(%rip)      # 0x6160
pushq $0xa
popq %rbx
subl $0x1, %ebx
jb 0x14bd
movq %r12, %rdi
movq %r15, %rsi
movl -0x34(%rbp), %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl -0x2c(%rbp), %r9d
pushq %r14
pushq %r13
callq 0x2eb0
addq $0x10, %rsp
jmp 0x1497
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
callq 0x1110
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
subq 0x4ce9(%rip), %rax      # 0x61c0
cvtsi2sd %rax, %xmm0
subq 0x4ce5(%rip), %rcx      # 0x61c8
cvtsi2sd %rcx, %xmm1
mulsd 0x2b18(%rip), %xmm1     # 0x4008
mulsd 0x2b18(%rip), %xmm0     # 0x4010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x2b08(%rip), %xmm0     # 0x4018
cvttsd2si %xmm0, %rdx
andl %ecx, %edx
orl %edx, %eax
movl 0x4c41(%rip), %ecx      # 0x6160
addl %ecx, %eax
incl %eax
movl %eax, 0x4c37(%rip)      # 0x6160
leaq 0x4c34(%rip), %rdi      # 0x6164
leaq 0x4c29(%rip), %rsi      # 0x6160
pushq $0x1
popq %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl -0x2c(%rbp), %r9d
pushq %r14
pushq %r13
callq 0x2eb0
popq %rax
popq %rcx
cmpl $0x0, -0x30(%rbp)
movl -0x34(%rbp), %ebx
jne 0x1574
movl 0x4c07(%rip), %eax      # 0x6164
xorl %edx, %edx
divl -0x38(%rbp)
leaq 0x2b01(%rip), %rdi      # 0x406a
movl %ebx, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x10e0
movl %ebx, %r15d
xorl %r13d, %r13d
xorl %ebx, %ebx
xorl %r14d, %r14d
cmpq %rbx, %r15
je 0x15af
movl (%r12,%rbx,4), %ecx
cmpl %ecx, %r13d
je 0x15a6
leaq 0x2adf(%rip), %rdi      # 0x4073
movl -0x30(%rbp), %esi
movl %ebx, %edx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x10e0
incl %r14d
incq %rbx
addl -0x2c(%rbp), %r13d
jmp 0x157f
testl %r14d, %r14d
je 0x15c8
leaq 0x2acc(%rip), %rdi      # 0x4087
movl -0x30(%rbp), %esi
movl %r14d, %edx
xorl %eax, %eax
callq 0x10e0
movl -0x34(%rbp), %ecx
addl %ecx, %ecx
leaq 0x4b9c(%rip), %r14      # 0x6170
movq -0x58(%rbp), %r15
movq -0x50(%rbp), %r13
jmp 0x13f7
movq %r12, %rdi
callq 0x1614
movq %r15, %rdi
callq 0x1614
callq 0x1c25
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop