Created
April 29, 2013 14:29
-
-
Save Novakov/5481921 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <limits.h> | |
#include "pvm3.h" | |
#define COLS 4 | |
#define ROWS 4 | |
#define MSG_INIT 100 | |
#define MSG_RESPONSE 200 | |
#define MSG_DATA 300 | |
int recv(int * min, int * max) | |
{ | |
int buf[2]; | |
int bufid, tid; | |
bufid = pvm_recv(-1, MSG_RESPONSE); | |
pvm_bufinfo(bufid, NULL, NULL, &tid); | |
pvm_upkint(&buf, 2, 1); | |
printf("Received from %d: {%d,%d}\n", tid, buf[0], buf[1]); | |
if(buf[0] < *min) | |
{ | |
*min = buf[0]; | |
} | |
if(buf[1] > *max) | |
{ | |
*max = buf[1]; | |
} | |
return tid; | |
} | |
void send(int tid, int * matrix, int offset, int packSize) | |
{ | |
printf("Sending from %d to %d\n", offset, offset + packSize); | |
pvm_initsend(PvmDataDefault); | |
pvm_pkint(&packSize, 1, 1); | |
pvm_pkint(matrix + offset, packSize, 1); | |
pvm_send(tid, MSG_DATA); | |
} | |
void parent() | |
{ | |
int i, ilhost, ilarch; | |
struct pvmhostinfo *info; | |
int * tids; | |
int created = 0; | |
const int data[ROWS][COLS] = | |
{ | |
{4,3,2,1}, | |
{8,78,6,5}, | |
{12,-11,10,9}, | |
{16,15,14,13} | |
}; | |
int * matrix = data; | |
int min = INT_MAX; | |
int max = INT_MIN; | |
int len = ROWS * COLS; | |
int PACK_SIZE = 2; | |
printf("Parent\n"); | |
pvm_config (&ilhost, &ilarch, &info); | |
ilhost = 4; | |
tids = calloc(ilhost, sizeof(int)); | |
printf("ilhost: %d\n", ilhost); | |
for(i = 0; i < ilhost; i++) | |
{ | |
int e = pvm_spawn("/home/pvm/pvm3/sekcja2/bin/LINUX/hello", 0, PvmTaskHost, info[i].hi_name, 1, &tids[i]); | |
printf("Create child %d on %s = %d\n", i, info[i].hi_name, e); | |
created += e; | |
pvm_initsend (PvmDataDefault); | |
pvm_send(tids[i], MSG_INIT); | |
} | |
printf("Init data send\n"); | |
for(i = 0; i < created; i++) | |
{ | |
send(tids[i], matrix, i, PACK_SIZE); | |
} | |
for(i = ilhost * PACK_SIZE; i < len; i += PACK_SIZE) | |
{ | |
int tid = recv(&min, &max); | |
send(tid, matrix, i, PACK_SIZE); | |
} | |
for(i = 0; i < created; i++) | |
{ | |
int tid = recv(&min, &max); | |
pvm_kill(tid); | |
} | |
printf("Min = %d Max = %d\n", min, max); | |
} | |
void child() | |
{ | |
int tmp = 42; | |
printf("Child\n"); | |
pvm_recv(-1, MSG_INIT); | |
while(1) | |
{ | |
int packSize; | |
int * numbers; | |
int i; | |
int minmax[2] = {INT_MAX,INT_MIN}; | |
pvm_recv(-1, MSG_DATA); | |
pvm_upkint(&packSize, 1, 1); | |
numbers = calloc(packSize, sizeof(int)); | |
pvm_upkint(numbers, packSize, 1); | |
for(i = 0; i < packSize; i++) | |
{ | |
if(numbers[i] < minmax[0]) | |
minmax[0] = numbers[i]; | |
if(numbers[i] > minmax[1]) | |
minmax[1] = numbers[i]; | |
} | |
pvm_initsend(PvmDataDefault); | |
pvm_pkint(minmax, 2, 1); | |
pvm_send(pvm_parent(), MSG_RESPONSE); | |
} | |
} | |
int main() | |
{ | |
int tid; | |
tid = pvm_mytid(); | |
if(pvm_parent() == PvmNoParent) | |
{ | |
parent(); | |
} | |
else | |
{ | |
child(); | |
} | |
pvm_exit(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment