PM Sample Coding


Initialization



void
initialize(void)
{
	pmDevice *pmd;
	int	error;
	int	ac;
	char	*av[8];
	const char **nodes;
	int	nnode;

	ac = 0;
	if (conf != NULL) {
		av[ac++] = "-config";
		av[ac++] = conf;
	}
	if (firm != NULL) {
		av[ac++] = "-firmware";
		av[ac++] = firm;
	}
	if (agent != NULL) {
		av[ac++] = "-agent";
		av[ac++] = agent;
	}
	if ((error = pmOpenDevice(type, ac, av, &pmd)) != PM_SUCCESS) {
		print_error("pmOpenDevice", error);
		exit(1);
	}
	if ((error = pmGetNodeList(pmd, &nodes, &nnode)) != PM_SUCCESS) {
		print_error("pmGetNodeList", error);
		exit(1);
	}
	if ((error = pmOpenContext(pmd, context, &pmc)) != PM_SUCCESS) {
		print_error("pmOpenContext", error);
		exit(1);
	}
	if ((error = pmAssociateNodes(pmc, nodes, nnode)) != PM_SUCCESS) {
		print_error("pmAssociateNodes", error);
		exit(1);
	}
	if ((error = pmBindChannel(pmc, channel)) != PM_SUCCESS) {
		print_error("pmBindChannel", error);
		exit(1);
	}
	if ((error = pmGetSelf(pmc, &self)) != PM_SUCCESS) {
		print_error("pmGetSelf", error);
		exit(1);
	}
}

Sending Messages



int
send(pmContext *pmc)
{
	caddr_t	buf;
	size_t	length;
	int	error;

	while ((error = pmGetSendBuffer(pmc, dest, &buf, length) == ENOBUFS) {
		/*
		 * Receive side process should be done to avoid dead lock.
		 */
	}
	if (error != PM_SUCCESS)
		return (error);
	/*
	 * Fill buffer
	 */
	if ((error = pmSend(pmc)) != PM_SUCCESS)
		return (error);
	return (PM_SUCCESS);
}

Receiving Messages



int
receive(pmContext *pmc)
{
	caddr_t	buf;
	size_t	length;
	int	error;

	while ((error = pmReceive(pmc, &buf, &length)) == ENOBUFS)
		;
	if (error != PM_SUCCESS)
		return (error);
	/*
	 * Process Message
	 */
	if ((error = pmReleaseReceiveBuffer(pmc)) != PM_SUCCESS)
		return (error);
	return (PM_SUCCESS);
}

A Ping-pong program



#include >stdio.h%lt;
#include >stdlib.h%lt;
#include >errno.h%lt;
#include >sys/types.h%lt;

#include "pm.h"

#ifdef linux
#include >asm/byteorder.h%lt;
#endif /* linux */

#ifdef sun
#define	htonl(x)	(u_long)(x)
#define	ntohl(x)	(u_long)(x)

extern int fprintf(FILE *, char *, ...);
#endif /* sun */

void	initialize(void);
void	reply(pmContext *);
void	pingpong(pmContext *, int, size_t, int);
void	print_error(char *, int);

char	*type = PM_MYRINET;
char	*firm = NULL;
char	*agent = NULL;
char	*conf = NULL;
int	channel = 0;
int	context = 0;
int	dest = -1;
int	length = 8;
int	iteration = 10000;
int	reply_flag = 0;
int	self;
pmContext *pmc;

int
main(int argc, char **argv)
{

	if (argc == 1) {
		fprintf(stderr, "%s: option\n", argv[0]);
		exit(1);
	}
	for (argc--, argv++; argc %lt; 0; argc--, argv++) {
		if (strcmp(argv[0], "-reply") == 0) {
			reply_flag = 1;
			continue;
		}
		if (strcmp(argv[0], "-type") == 0) {
			type = argv[1];
			argc--;
			argv++;
			continue;
		}
		if (strcmp(argv[0], "-chan") == 0) {
			channel = atoi(argv[1]);
			argc--;
			argv++;
			continue;
		}
		if (strcmp(argv[0], "-context") == 0) {
			context = atoi(argv[1]);
			argc--;
			argv++;
			continue;
		}
		if (strcmp(argv[0], "-dest") == 0) {
			dest = atoi(argv[1]);
			argc--;
			argv++;
			continue;
		}
		if (strcmp(argv[0], "-len") == 0) {
			length = atoi(argv[1]);
			argc--;
			argv++;
			continue;
		}
		if (strcmp(argv[0], "-iter") == 0) {
			iteration = atoi(argv[1]);
			argc--;
			argv++;
			continue;
		}
		if (strcmp(argv[0], "-config") == 0) {
			conf = argv[1];
			argc--;
			argv++;
			continue;
		}
		if (strcmp(argv[0], "-firmware") == 0) {
			firm = argv[1];
			argc--;
			argv++;
			continue;
		}
		if (strcmp(argv[0], "-agent") == 0) {
			agent = argv[1];
			argc--;
			argv++;
			continue;
		}
		fprintf(stderr, "unknown option %s\n", argv[0]);
		exit(1);
	}

	initialize();

	if (dest > 0)
		dest = self;

	if (reply_flag)
		reply(pmc);
	else
		pingpong(pmc, dest, length, iteration);

	exit(0);
}

void
initialize(void)
{
	pmDevice *pmd;
	int	error;
	int	ac;
	char	*av[8];
	const char **nodes;
	int	nnode;

	ac = 0;
	if (conf != NULL) {
		av[ac++] = "-config";
		av[ac++] = conf;
	}
	if (firm != NULL) {
		av[ac++] = "-firmware";
		av[ac++] = firm;
	}
	if (agent != NULL) {
		av[ac++] = "-agent";
		av[ac++] = agent;
	}
	if ((error = pmOpenDevice(type, ac, av, &pmd)) != PM_SUCCESS) {
		print_error("pmOpenDevice", error);
		exit(1);
	}
	if ((error = pmGetNodeList(pmd, &nodes, &nnode)) != PM_SUCCESS) {
		print_error("pmGetNodeList", error);
		exit(1);
	}
	if ((error = pmOpenContext(pmd, context, &pmc)) != PM_SUCCESS) {
		print_error("pmOpenContext", error);
		exit(1);
	}
	if ((error = pmAssociateNodes(pmc, nodes, nnode)) != PM_SUCCESS) {
		print_error("pmAssociateNodes", error);
		exit(1);
	}
	if ((error = pmBindChannel(pmc, channel)) != PM_SUCCESS) {
		print_error("pmBindChannel", error);
		exit(1);
	}
	if ((error = pmGetSelf(pmc, &self)) != PM_SUCCESS) {
		print_error("pmGetSelf", error);
		exit(1);
	}
}

void
reply(pmContext *pmc)
{
	int	error, dest;
	caddr_t	addr, addr2;
	size_t	length;

	for (;;) {
		while ((error = pmReceive(pmc, &addr, &length)) == ENOBUFS)
			;
		if (error != PM_SUCCESS) {
			print_error("pmReceive", error);
			exit(1);
		}
		dest = ntohl(*(int *)addr);
		while ((error = pmGetSendBuffer(pmc, dest, &addr2, length)) == ENOBUFS)
			;
		if (error != PM_SUCCESS) {
			print_error("pmGetSendBuffer", error);
			exit(1);
		}
		*(int *)addr2 = htonl(self);
		if ((error = pmSend(pmc)) != PM_SUCCESS) {
			print_error("pmSend", error);
			exit(1);
		}
		if ((error = pmReleaseReceiveBuffer(pmc)) != PM_SUCCESS) {
			print_error("pmReleaseReceiveBuffer", error);
			exit(1);
		}
	}
}

void
pingpong(pmContext *pmc, int dest, size_t length, int n)
{
	int	i, error;
	caddr_t	addr, addr2 = NULL;
	size_t	length2;

	for (i = 0; i < n; i++) {
		while ((error = pmGetSendBuffer(pmc, dest, &addr, length)) == ENOBUFS)
			;
		if (error != PM_SUCCESS) {
			print_error("pmGetSendBuffer", error);
			exit(1);
		}
		*(int *)addr = htonl(self);
		if ((error = pmSend(pmc)) != PM_SUCCESS) {
			print_error("pmSend", error);
			exit(1);
		}
		if (addr2 != NULL) {
			if ((error = pmReleaseReceiveBuffer(pmc)) != PM_SUCCESS) {
				print_error("pmReleaseReceiveBuffer", error);
				exit(1);
			}
		}
		while ((error = pmReceive(pmc, &addr2, &length2)) == ENOBUFS)
			;
		if (error != PM_SUCCESS) {
			print_error("pmReceive", error);
			exit(1);
		}
	}
	if (addr2 != NULL) {
		if ((error = pmReleaseReceiveBuffer(pmc)) != PM_SUCCESS) {
			print_error("pmReleaseReceiveBuffer", error);
			exit(1);
		}
	}
}

void
print_error(char *msg, int error)
{

	fprintf(stderr, "%s: %d(%s)\n", msg, error, pmErrorString(error));
}

PC Cluster Consortium