// File: /workspace/github_c_makefile_star2_O3/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, 0x4dd7(%rip)     # 0x61b0
movapd %xmm0, 0x4dbf(%rip)     # 0x61a0
movapd %xmm0, 0x4da7(%rip)     # 0x6190
movapd %xmm0, 0x4d8f(%rip)     # 0x6180
movapd %xmm0, 0x4d77(%rip)     # 0x6170
callq 0x16cc
leaq 0x4d33(%rip), %rax      # 0x6138
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
leaq 0x4d2b(%rip), %rax      # 0x613c
movl (%rax), %ebx
movl $0x7d0000, %edi         # imm = 0x7D0000
callq 0x1cd0
movq %rax, %r15
movl $0x7d0000, %edi         # imm = 0x7D0000
callq 0x1cd0
movq %rax, -0x38(%rbp)
movl $0x3e8fa0, %edi         # imm = 0x3E8FA0
callq 0x1cd0
movq %rax, %r12
xorl %eax, %eax
movl %eax, (%r15,%rax,4)
incq %rax
cmpq $0x800, %rax            # imm = 0x800
jne 0x143d
cmpl $0x0, -0x2c(%rbp)
movq -0x38(%rbp), %rdi
jne 0x146a
leaq 0x2bc3(%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 0x148c
callq 0x16a8
leaq 0x4d16(%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 0x4cf5(%rip), %rax      # 0x61c0
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
subq 0x4cee(%rip), %rcx      # 0x61c8
cvtsi2sd %rcx, %xmm1
mulsd 0x2b21(%rip), %xmm1     # 0x4008
mulsd 0x2b21(%rip), %xmm0     # 0x4010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x2b11(%rip), %xmm0     # 0x4018
cvttsd2si %xmm0, %rdx
andl %ecx, %edx
orl %edx, %eax
notl %eax
movl %eax, 0x4c48(%rip)      # 0x6160
movl $0xa, %ebx
leaq 0x4c4c(%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 0x30d0
addq $0x10, %rsp
decl %ebx
jne 0x1524
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
callq 0x1110
movq -0x58(%rbp), %rax
movq -0x50(%rbp), %rcx
subq 0x4c5f(%rip), %rax      # 0x61c0
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
subq 0x4c58(%rip), %rcx      # 0x61c8
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
mulsd 0x2a88(%rip), %xmm1     # 0x4008
mulsd 0x2a88(%rip), %xmm0     # 0x4010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
sarq $0x3f, %rcx
subsd 0x2a78(%rip), %xmm0     # 0x4018
cvttsd2si %xmm0, %rdx
andl %ecx, %edx
orl %edx, %eax
movl 0x4bb1(%rip), %ecx      # 0x6160
addl %ecx, %eax
incl %eax
movl %eax, 0x4ba7(%rip)      # 0x6160
leaq 0x4ba4(%rip), %rdi      # 0x6164
leaq 0x4b99(%rip), %rsi      # 0x6160
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movl %r14d, %r9d
pushq %r13
pushq %r12
callq 0x30d0
addq $0x10, %rsp
cmpl $0x0, -0x2c(%rbp)
movl -0x30(%rbp), %r15d
jne 0x1609
movl 0x4b73(%rip), %eax      # 0x6164
xorl %edx, %edx
divl -0x3c(%rbp)
leaq 0x2a6d(%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 0x163d
leaq 0x2a4c(%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 0x1618
testl %r14d, %r14d
je 0x1666
leaq 0x2a32(%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 0x1484
callq 0x16b4
movq %r15, %rdi
callq 0x16b4
callq 0x1ccd
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)