Saturday, August 8, 2009

Poor Man's Cluster - Step 5

Poor Man's Cluster - Step 5
Topic: LinuxClusters

Now, to do a rendering, you have to be logged into the master as "cluster."  Also, you have to download and install the latest version of povray (we used 3.6) on every worker node.  We also saved temporary files in /home/cluster/distimg, so make this directory on every worker node.  Finally, the following c program, chop.c, needs to be stored on the path of every worker, we put it into /bin as root. 
To actually start a job we used the following commandline:
time ./chopsplit.sh advanced/biscuit.pov 400 300 biscuit.ppm 1 2 3 ...
if we wanted to render biscuit.pov on nodes 1 2 3.  That's really 10.5.129.1, 10.5.129.2, 10.5.129.3 , ....  Then we converted biscuit.ppm to a jpg using the gimp.  The 400 is the horizontal pixel resolution and the 300 is the vertical pixel resolution in this example.
Type the code that follows using any text editor, call it "chop.c" and compile it with the commandline: gcc -o chop chop.c
#include
#include
#include
#include
#include
#include
#define MAX_UINT ((unsigned)-1)
#define MAX_INT (MAX_UINT>>1)
#define MIN_INT (MAX_INT+1)
//Almost as fast a #defines, no side effects
inline int max(int a, int b)
{
if (a>b)
return a;
return b;
}
inline int min(int a, int b)
{
if (a
return a;
return b;
}
int main(int argc, char *argv[])
{
struct stat stats;
int infile, outfile, bs, fbyte, lbyte, fpos=0;
char *buf;
int x, y, off;
if (argc<3)
{
printf("%s usage: filein fileout [firstbyte [lastbyte]]\n", argv[0]);
exit(1);
}
//if argv[1]=="-"
if (argv[1][0]=='-' && argv[1][1]=='\0')
infile=STDIN_FILENO;
else
{
infile=open(argv[1], O_RDONLY);
if (infile==-1)
{
fprintf(stderr, "Error reading input file. Quiting.\n");
exit(2);
}
}
//if argv[2]=="-"
if (argv[2][0]=='-' && argv[2][1]=='\0')
outfile=STDOUT_FILENO;
else
{
outfile=open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH /*644*/);
if (outfile==-1)
{
fprintf(stderr, "Error reading output file. Quiting.\n");
perror(NULL);
exit(2);
}
}
if (argc>3)
{
fbyte=atol(argv[3]);
if (argc>4)
lbyte=atol(argv[4]);
else
lbyte=0;
}
else
lbyte=fbyte=0;
if(infile==STDIN_FILENO || fstat(infile, &stats))
{
//Cannot get file size, defaulting to 64k
bs=64*1024;
if (fbyte<0)
fbyte=0;
if (lbyte<0)
lbyte=0;
}
else
{
bs=stats.st_size;
if (fbyte<0)
fbyte+=bs;
if (lbyte<0)
lbyte+=bs;
}
buf=malloc(bs);
while (buf==0)
{
bs/=2;
if (bs==0)
{
fprintf(stderr, "No memory available, quitting\n");
exit(3);
}
buf=malloc(bs);
}
if (lbyte==0)
lbyte=MAX_INT;
fpos=lseek(infile, fbyte, SEEK_SET);
if (fpos==-1)
{
if (errno==ESPIPE)
{
fpos=0;
while (fpos
{
x=read(infile, buf, min(bs,fbyte-fpos));
//EOF before fbyte
if (x==0)
exit(4);
if (x==-1)
{
fprintf(stderr, "Read failure. Quiting");
exit(5);
}
fpos+=x;
}
}
else
{
fprintf(stderr, "Seek error. Quitting\n");
exit(6);
}
}
while (fpos
{
x=read(infile, buf, min(bs,lbyte-fpos));
if (x==0)
exit(0);
if (x==-1)
{
fprintf(stderr, "Read error. Quiting\n");
exit(6);
}
fpos+=x;
off=0;
while(x>0)
{
y=write(outfile, buf+off, x);
if (y==-1)
{
fprintf(stderr, "Write error. Quiting\n");
exit(7);
}
x-=y;
off+=y;
}
}
return 0;
}

TTYL,

No comments:

Post a Comment