Parallel Programming
on the Macintosh



Timothy H. Kaiser
tkaiser@sdsc.edu

University of New Mexico
High Performance Computing
Education and Research Center
Albuquerque Resource Center

Introduction

Why am I here?
Why parallel program on the Macintosh?
What we have available?
Introduction to Tenon/Mach
Introduction to p4
Introduction to MPI
Porting issues under Mach
Running p4 and MPI under Mach
Running Macintosh native p4

Why am I here?

Ph.D. candidate in Computer Science
MS - EE (Applied Physics)
BS - physics
RA Albuquerque Resource Center
Dissertation topic: Parallelization strategies for adaptive grid programs
SAIC
Operations Analysis Group
Applied Physics Group - large scale simulation
Macintosh Application development
Adaptive grid visualization
Portable yield estimation
Fourier analysis

Why parallel program on the Macintosh?

Pernode performance greater than SP1
100 x 100 matrix inversion 100 times
gcc -o invert -O3 invert.c
Low end PPC Macintosh = 12.93 seconds
SP1 node = 12 seconds
SP1 node = 9 seconds with IBM compiler
Availability
Cost
Good development tools
Symantec - Think C
Metrowerks - Code Warrior
F90 will be in beta test in December

What we have available

Message passing systems for distributed Macs
MPI - MPICH running under Tenon/Mach
P4 - also running under Tenon/Mach
P4 - subset running under native Macintosh OS
Do not have anything for the shared memory Macintosh

Overview of Tenon/Mach

A Unix application for the Mac
Features
Most Unix utilities
Multitasking
C, Fortran77
X-window support (fast)
Mach application support for 68k Macs
Good performance for compiled programs
Hope to port Nag Fortran90 conpiler

Availability

Tenon at http://www.tenon.com/machine.html
Support for both PPC and 68k Macs
Pricing
$695 for full package
About half price for universities

Problems

Missing Mach application support for PPC
Program startup slow
Man is very slow
Not 100% stable
X-windows
Sockets
Linker bugs
xdr routines are not complete

Overview of p4

Simple message passing system developed at Argonne
Small number of calls
Fortran and C
Generally available for Unix systems
rsh
sockets
Source available from:
ftp.arc.unm.edu
/pub/Parallel_Prog_Sys_info/Message_Pass_Sys/p4
http://www.mcs.anl.gov

Example p4 program

#include 
#include 
#include 
#include "p4.h"

double f(a)
double a;
{
    return (4.0 / (1.0 + a*a));
}

main(int argc, char ** argv)
{
	double h,sum,x,mypi;
	int ijk,type,from,size,myid,n,numprocs,jid;
    ijk=p4_initenv(&argc,argv)
    ijk=p4_create_procgroup()
    myid=p4_get_my_id();
    numprocs=p4_num_total_ids();
    if(myid == 0){
        n=1000;
        printf("enter the number of iterations: ");
        scanf("%d",&n);
        for (ijk=1;ijk<=numprocs-1;ijk++)
            p4_send(1,ijk,(char*)&n,sizeof(int));
    }
    else {
        type=-1;
        from=-1;
        size=sizeof(int);
        p4_recv(&type,&from,(char**)&n,&size);
    }
    h   = 1.0 / (double) n;
    sum = 0.0;
    for (ijk = myid + 1; ijk <= n; ijk += numprocs) {
        x = h * ((double)ijk - 0.5);
        sum += f(x);
    }
    mypi = h * sum;
    type=2;
    size=sizeof(double);
    if(myid == 0){
       for (ijk=1;ijk<=numprocs-1;ijk++){
            jid=ijk;
            p4_recv(&type,&jid,(char**)&x,&size);
            mypi=mypi+x;
       }
       printf("pi = %g\n",mypi);
    }
    else {
        p4_send(type,0,(char*)&mypi,size);
    }
    p4_wait_for_end();   
}

Overview of MPI

Extensive message passing system
Standard developed by Message Passing Interface Fourm
Protable
Fortran and C

We use MPICH version of MPI

Created at Argonne and Mississippi State
Source available at:
ftp.arc.unm.edu
/pub/Parallel_Prog_Sys_info/Message_Pass_Sys/mpi/mpich
http://www.mcs.anl.gov/mpi/mpich/index.html
Compiles to p4
Can also compile to MPL for SP2

Example MPI program

#include "mpi.h"
#include 
#include 

double f(a)
double a;
{
    return (4.0 / (1.0 + a*a));
}

int main(argc,argv)
int argc;
char *argv[];
{
    int  n, myid, numprocs, i, rc;
    double mypi, pi, h, sum, x;

    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
    MPI_Comm_rank(MPI_COMM_WORLD,&myid);
    if (myid == 0)
        n=10000;
    MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
    h   = 1.0 / (double) n;
    sum = 0.0;
    for (i = myid + 1; i <= n; i += numprocs){
        x = h * ((double)i - 0.5);
        sum += f(x);
    }
    mypi = h * sum;
    MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
    if (myid == 0)
        printf("pi is approximately %.16f\n",pi);
    MPI_Finalize();
}

Porting issues

Used MPICH 1.10 not 1.11
Minor changes to NeXT implementation
xdr_float.c is missing from Tenon Mach
Loader bug requires the MPI library be broken into sections

Changes to Makefiles and Source

mpich/configure
remove top blank lines
mpich/src/env/Makefile.in
$(CC) $(CFLAGS) -c -DCONFIGURE_ARGS_CLEAN \

mpich/mpid/ch_p4/p4-1.4/lib/p4_secure.c   
/* extern char *sys_errlist[]; */

mpich/mpid/ch_p4/p4-1.4/lib/p4_debug.c
#define VPRINTF

mpich/mpid/ch_p4/p4init.c
/* #include  */
#if defined(solaris) || defined(HAVE_SYSINFO)
    sysinfo( SI_SRPC_DOMAIN,name+l,nlen-l);
#elif defined(HAVE_GETDOMAINNAME)
    getdomainname( name+l, nlen - l );
#endif
  becomes
    getdomainname( name+l, nlen - l );

Running p4 & MPICH under Mach

Write, compile, link program
Uses C or Fortran include files
Link with MPI or p4 library
Create process group file
Specifies which machines and programs to run
Run Program indicating process group file on command line
pi.exe -p4pg pi.pg

Example p4 makefile

P4_HOME_DIR =/home/tkaiser/p4-1.4
INCLUDEDIR = $(P4_HOME_DIR)/include
LIBDIR = $(P4_HOME_DIR)/lib
LIBS = $(LIBDIR)/libp4.a $(MDEP_LIBS)
CLINKER = cc 
CFLAGS = -g -I$(INCLUDEDIR) $(MDEP_CFLAGS) $(USER_CFLAGS)
MDEP_LIBS = -lrpc /home/tkaiser/mpich/lib/NeXT/ch_p4/xdr_float.o


.c.o:$(P)
	$(CC) $(CFLAGS) -c $*.c 

bcast:$(P) bcast.o  $(LIBDIR)/libp4.a
	$(CLINKER) $(CFLAGS) -o bcast bcast.o  $(LIBS)


Example MPI makefile

SHELL       = /bin/sh
ARCH        = NeXT
COMM        = ch_p4
MPIR_HOME   = /home/tkaiser/mpich
CC          = cc
CLINKER     = cc
LIB_PATH    = -L$(MPIR_HOME)/lib/$(ARCH)/$(COMM)
LIB_LIST    = -lmpi -lrpc $(MPIR_HOME)/lib/$(ARCH)/$(COMM)/*.o
INCLUDE_DIR = -I$(MPIR_HOME)/include 
CFLAGS	  =  -DMPID_NO_FORTRAN  -DHAS_XDR=1 -DSTDC_HEADERS=1
-DHAVE_STDLIB_H=1 -DMALLOC_RET_VOID=1 -DHAVE_SYSTEM=1
-DHAVE_NICE=1 -DHAVE_LONG_DOUBLE=1 -DHAVE_LONG_LONG_INT=1
$(OPTFLAGS) $(INCLUDE_DIR) -DMPI_$(ARCH) 
MPILIB	  = $(MPIR_HOME)/lib/$(ARCH)/$(COMM)/libmpi.a 
LIBS	  = $(LIB_PATH) $(LIB_LIST)


cpi: cpi.o $(MPIR_HOME)/include/mpir.h $(MPILIB)
	$(CLINKER) $(OPTFLAGS) -o cpi cpi.o \
	$(LIB_PATH) $(LIB_LIST) -lm
.c.o:
	$(CC) $(CFLAGS) -c $*.c


Example p4pg file

local 0
osage.arc.unm.edu 1 /home/tkaiser/p4-1.4/contrib/bcast
cochiti.arc.unm.edu 1 /home/tkaiser/p4/SGI/bcast
acoma.arc.unm.edu 1 /home/tkaiser/p4/RS6000/bcast

Why run native

Cheaper
Not tied to Unix
Better code development tools

Problems in running native

P4 and MPI not written for Mac
Convoluted with makefiles and setup scripts
Many #ifdefs
No Unix rsh
No Unix sockets
Very difficult time getting Mac native IPC to work

My work arounds for problems

Simulate rsh with Applescript
GUSI utilities simulate sockets
Written routines call compatable with original p4
Message passing routines
Have core routines written

How Unix starts p4 jobs

Get name of p4pg file from command line
Read p4pg file for full path name of application
Do rsh to each of the machines to launch jobs
Set up sockets

Overview of Applescript

Scripting mechanism for Macintosh
Syntax Similar to Hypertalk
Scripts can be compiled to applications
Purpose: automate tasks
Copy, move, launch files
Send commands to programs
Requirements
AppleScript Extension
Finder Scripting Extension
System 7.5?

What p4_startup Applescript does

Open and read p4pg file
Full path name of each application
Macintosh machine name
FTP address
Copy p4pg file to preference folder of every machine
Launches application on every machine

Format of p4pg file

One line per application
No blanks in any names
Macintosh Name
Ftp name
Full pathname to application

Example Macintosh p4pg file

alice   classic.arc.unm.edu hd:pi.68k
ozark   ozark.arc.unm.edu   pb520hd:pi.68k
osage   osage.arc.unm.edu   hd:pi.PPC

The p4_startup script

-- script to send a preference file and launch a remote job
set filespec to choose file with prompt "Select Process Group File"
set p5_file to (open for access filespec without write permission)
set mac_list to ""
set ftp_list to ""
set app_list to {}
set mlist to ""
set the_count to 0
set theline to "continue"
repeat while theline  ""
	try
		set mac_name to " "
		repeat while mac_name = " "
			set mac_name to read p5_file until " "
		end repeat
		set ftp_name to " "
		repeat while ftp_name = " "
			set ftp_name to read p5_file until " "
		end repeat
		set app_name to read p5_file before return as string
		--display dialog app_name
		repeat while character 1 of app_name is " "
			set z to number of characters of app_name
			set app_name to subString(2, z, app_name)
		end repeat
		set mlist to mlist & mac_name & " " & ftp_name & " " & app_name & return
		if theline  "" then
			set the_count to the_count + 1
			set mac_list to mac_list & " " & mac_name
			set ftp_list to ftp_list & " " & ftp_name
			set app_list to app_list & app_name
		end if
	on error
		set theline to ""
	end try
end repeat

set launched to 0
repeat with i from 1 to the_count
	set machine_name to word i of mac_list
	set app_name to "" & item i of app_list
	set launched to launched + open_remote(machine_name, app_name, mlist)
end repeat
if launched  the_count then
	repeat with i from 1 to the_count - launched
		beep
	end repeat
end if


on open_remote(machine_name, app_path, machine_list)
	tell application "Finder" of machine machine_name
		activate
		try
			set the_path to path to preferences folder as string
			set the_path to the_path & "p4.pg"
			set p4_file to (open for access file the_path with write permission)
			set eof p4_file to 0
			write machine_list to p4_file
			close access p4_file
			open file app_path
			set worked to 1
		on error
			set worked to 0
		end try
	end tell
	return worked
end open_remote


on subString(x, y, theString)
	return (characters x thru y of theString) as string
end subString

GUSI overview

What: Grand Unified Socket Interface
Purpose: Provide simple and consistent interface to files, Appletalk, Native IPC, Internet, Printer
Access Protocal
Who: Matthias Neeracher , neeri@iis.ee.ethz.ch
Where: ftp.switch.ch sortware/mac/src/mpw_c
Supported:
PowerMac & 68k
Metrowerks C, MPW
We use the Unix style sockets

What happens in Mac p4 application

Read preference file with ip names
Requires Ethernet connection and name server
Converts ip name to ip address
Assign process id
Create an array of sockets
Open sockets to each Macintosh application
Read & write sockets (blocking and nonblocking)

Supported p4 calls


p4_initenv(&argc,argv);                    Init p4
p4_create_procgroup();                     Init p4
p4_get_my_id();                            Get my id
p4_num_total_ids();                        Number of processors
p4_send(1,ijk,(char*)&n,sizeof(int));      Nonblocking send
p4_recv(&type,&from,(char**)&n,&size);     Blocking Receive
p4_wait_for_end();                         Termination   

Will support all message passing calls

Challenges in native p4 port using GUSI

No major problems, many headaches
No machines to use in testing
Out of order messages
Requires buffering of incoming messages
Check list of old messages before new
Send to self also requires buffering
Wild cards
Many different cases
Still has bugs

Creating p4 application with Metrowerks C

Create Project
Use precompiled headers:
Add Libraries
Write in C using supported p4 calls
Do for both 68k and PPC if needed
Move application to each machine
Create p4pg file
launch script p4_startup

Running the p4 application

Launch p4_startup script
Select p4pg file
Enter passwords
Let them run
For now, kill all jobs when finished

Future Plans

Native
Finish native p4 Port
Do native Fortran port
Port native MPI
Port to Apple IPC
Mach
Port version 1.11 of MPICH
Fortran port

The Albuquerque Resource Center

© Copyright 1995, The Maui Project/UNM
Last Revised: November 10, 1995
tkaiser@sdsc.edu