// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
xorpd %xmm0, %xmm0
movapd %xmm0, 0x4db7(%rip)     # 0x61b0
movapd %xmm0, 0x4d9f(%rip)     # 0x61a0
movapd %xmm0, 0x4d87(%rip)     # 0x6190
movapd %xmm0, 0x4d6f(%rip)     # 0x6180
movapd %xmm0, 0x4d57(%rip)     # 0x6170
callq 0x16ec
leaq 0x4d13(%rip), %rax      # 0x6138
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
leaq 0x4d0b(%rip), %rax      # 0x613c
movl (%rax), %ebx
movl $0x7d0000, %edi         # imm = 0x7D0000
callq 0x1cb4
movq %rax, %r15
movl $0x7d0000, %edi         # imm = 0x7D0000
callq 0x1cb4
movq %rax, -0x38(%rbp)
movl $0x3e8fa0, %edi         # imm = 0x3E8FA0
callq 0x1cb4
movq %rax, %r12
xorl %eax, %eax
movl %eax, (%r15,%rax,4)
incq %rax
cmpq $0x800, %rax            # imm = 0x800
jne 0x145d
cmpl $0x0, -0x2c(%rbp)
movq -0x38(%rbp), %rdi
jne 0x148a
leaq 0x2ba3(%rip), %rdi      # 0x4020
movl %ebx, %esi
xorl %eax, %eax
callq 0x10e0
movq -0x38(%rbp), %rdi
leal (%rbx,%rbx), %eax
leal (%rax,%rax,4), %eax
movl %eax, -0x3c(%rbp)
movl $0x1, %eax
movq %r15, -0x68(%rbp)
movq %rbx, -0x48(%rbp)
movq %r12, -0x60(%rbp)
movq %rbx, %r14
movl %eax, -0x30(%rbp)
xorl %eax, %eax
movl $0xffffffa6, (%rdi,%rax,4) # imm = 0xFFFFFFA6
incq %rax
cmpq $0x800, %rax            # imm = 0x800
jne 0x14ac
callq 0x16c8
leaq 0x4cf6(%rip), %rdi      # 0x61c0
xorl %esi, %esi
callq 0x1110
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
callq 0x1110
movq -0x58(%rbp), %rax
movq -0x50(%rbp), %rcx
subq 0x4cd5(%rip), %rax      # 0x61c0
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
subq 0x4cce(%rip), %rcx      # 0x61c8
cvtsi2sd %rcx, %xmm1
mulsd 0x2b01(%rip), %xmm1     # 0x4008
mulsd 0x2b01(%rip), %xmm0     # 0x4010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x2af1(%rip), %xmm0     # 0x4018
cvttsd2si %xmm0, %rdx
andl %ecx, %edx
orl %edx, %eax
notl %eax
movl %eax, 0x4c28(%rip)      # 0x6160
movl $0xa, %ebx
leaq 0x4c2c(%rip), %r13      # 0x6170
movq -0x38(%rbp), %rdi
movq %r15, %rsi
movl -0x30(%rbp), %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl %r14d, %r9d
pushq %r13
pushq %r12
callq 0x3008
addq $0x10, %rsp
decl %ebx
jne 0x1544
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
callq 0x1110
movq -0x58(%rbp), %rax
movq -0x50(%rbp), %rcx
subq 0x4c3f(%rip), %rax      # 0x61c0
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
subq 0x4c38(%rip), %rcx      # 0x61c8
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
mulsd 0x2a68(%rip), %xmm1     # 0x4008
mulsd 0x2a68(%rip), %xmm0     # 0x4010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x2a58(%rip), %xmm0     # 0x4018
cvttsd2si %xmm0, %rdx
andl %ecx, %edx
orl %edx, %eax
movl 0x4b91(%rip), %ecx      # 0x6160
addl %ecx, %eax
incl %eax
movl %eax, 0x4b87(%rip)      # 0x6160
leaq 0x4b84(%rip), %rdi      # 0x6164
leaq 0x4b79(%rip), %rsi      # 0x6160
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl %r14d, %r9d
pushq %r13
pushq %r12
callq 0x3008
addq $0x10, %rsp
cmpl $0x0, -0x2c(%rbp)
movl -0x30(%rbp), %r15d
jne 0x1629
movl 0x4b53(%rip), %eax      # 0x6164
xorl %edx, %edx
divl -0x3c(%rbp)
leaq 0x2a4d(%rip), %rdi      # 0x406a
movl %r15d, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x10e0
movl %r15d, %r15d
xorl %r13d, %r13d
xorl %ebx, %ebx
xorl %r14d, %r14d
movq -0x38(%rbp), %rdi
movl (%rdi,%rbx,4), %ecx
cmpl %ecx, %r13d
je 0x165d
leaq 0x2a2c(%rip), %rdi      # 0x4073
movl -0x2c(%rbp), %esi
movl %ebx, %edx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x10e0
movq -0x38(%rbp), %rdi
incl %r14d
incq %rbx
addl -0x48(%rbp), %r13d
cmpq %rbx, %r15
jne 0x1638
testl %r14d, %r14d
je 0x1686
leaq 0x2a12(%rip), %rdi      # 0x4087
movl -0x2c(%rbp), %esi
movl %r14d, %edx
xorl %eax, %eax
callq 0x10e0
movq -0x38(%rbp), %rdi
movl -0x30(%rbp), %eax
addl %eax, %eax
cmpl $0x801, %eax            # imm = 0x801
movq -0x68(%rbp), %r15
movq -0x48(%rbp), %rbx
movq -0x60(%rbp), %r12
jl 0x14a4
callq 0x16d4
movq %r15, %rdi
callq 0x16d4
callq 0x1cb2
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)