#include<assert.h>

#pragma TASS input 
double n;

/* This is an implementation of allreduce for the addition operator.*/
void allreduce(int length, double sendbuf[], double recvbuf[]){
	int i;
	int j;
	
	if(PID == 0){
		for(i = 1; i < NPROCS; i++){  // Maybe use special tag to indicate these are the send/recv for allreduce
			// Possibly use something like "input ALLREDUCETAG"
			recv(recvbuf, i, 0);
			for(j = 0; j < length; j++){
				sendbuf[j] = sendbuf[j] + recvbuf[j];
			}
		}
		for(i = 1; i < NPROCS; i++){
			send(sendbuf, i, 0);
		}
		for(j = 0; j < length; j++){
			recvbuf[j] = sendbuf[j];
		}
	}
	else{
		send(sendbuf, 0, 0);
		recv(recvbuf, 0, 0);
	}
}

void main() {
	double a[1];
	double b[1];
	
	a[0] = n;
	allreduce(1, a, b);
	assert(b[0] == n*NPROCS);
}