/*
 *             	    Exemple "Master1.c" PVM versio 3.3
 *	    	Susana Maria Bajo Sanchez (ei05646@salleURL.edu)
 *	    	Josep Maria Garrell i Guiu (josepmg@salleURL.edu)
 *		 	      Software Paral.lel
 *		         Departament d'Informatica (DI)
 *	               Enginyeria i Arquitectura La Salle
 *		         Universitat Ramon Llull (URL)
 * 			        Curs 1999/2000
*/

#include <stdio.h>
#include "pvm3.h"

int mytid;
int tids[32];
int n, nproc, i, who, msgtype, cc;
float data[100], result[32];

//---------------------------------------------------------------------------
void initialize_data (float data[100],int n)
{
	for (i=0;i<100;i++) data[i]=n;
}

//---------------------------------------------------------------------------
void main()
{
	// salida estandar por pantalla
	pvm_catchout(stdout);

	mytid = pvm_mytid();
	printf("Master1: Soy la tarea #%x\n",mytid);

	// inicio de todas las tareas "slaves"
	puts("¿Cuantos programas 'SLAVES' quieres (1-32)?");
	scanf("%d",&nproc);
	
	cc = pvm_spawn("Slave1",(char**)0,0,"",nproc,tids);
	if (cc==nproc)
		{
		printf("Master1: He podido crear las %d tareas 'Slave1'\n",nproc);

		// inicio del programa
		n = 1;
		initialize_data(data,n);
		
		// broadcast del dato inicial a las tareas "slaves"
		pvm_initsend(PvmDataRaw);
		pvm_pkint(&nproc,1,1);
		pvm_pkint(tids,nproc,1);
		pvm_pkint(&n,1,1);
		pvm_pkfloat(data,n,1);
		pvm_mcast(tids,nproc,0);

		// esperar los resultados de los "slaves"
		msgtype = 5;
		for (i=0;i<nproc;i++)
			{
			pvm_recv(-1,msgtype);
			pvm_upkint(&who,1,1);
			pvm_upkfloat(&result[who],1,1);
			printf("Yo tengo %x de %d\n", result[who],who);
			}
		}
		else printf("Master1: No he podido crear las tareas 'Slaves1'\n");

	printf("Master1: Salgo de PVM\n");
	pvm_exit();
}
