/************************************************************************/ /************************************************************************/ /* Matrix Multiplication for parallel algorithm */ /* Last update date : May,1, 2000 */ /* Programmed by : Sangmi Lee(slee@csit.fsu.edu) */ /************************************************************************/ /* How to compile : compile Mpaint class and then Paint class */ /* separately */ /* How to run : run Paint class */ /************************************************************************/ /************************************************************************/ /************* Paint.java **********************************************/ /* This is the class for layout and user interface */ /************************************************************************/ import java.awt.*; import java.awt.event.*; import java.applet.*; public class Paint extends Applet implements ActionListener // must overwrite actionPerformed() method { Label Status; Mpaint c; public void init() { setLayout(new BorderLayout()); setBackground(new Color(170,170,10)); setForeground(Color.black); Panel w=new Panel(); w.setLayout(new GridLayout(7,0)); Button b; w.add(b=new Button("Start")); b.addActionListener(this); // the applet is one of the listeners w.add(b=new Button("Fast")); b.addActionListener(this); // the applet is one of the listeners w.add(b=new Button("Slow")); b.addActionListener(this); // the applet is one of the listeners b.addActionListener(this); // the applet is one of the listeners add(BorderLayout.WEST, w); Status=new Label("Status Line"); Panel p=new Panel(); p.setBackground(Color.gray); p.add(Status); add(BorderLayout.SOUTH,p); c=new Mpaint(); c.setBackground(new Color(200,200,20)); c.curstep = 0; add(BorderLayout.CENTER, c); } public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); // We want to change the label of the button, // but getSource function returns an Object type object. // By type casting, we use it as if it is a button object. // Actually, it is a button, but getSource returns it // as an Object type object. // Note: We cannot use type casting if we are not sure // that the returning object is a button. In this case, // it is safe to use the following code: // if (e.getSource() instanceof Button) { // Button b=(Button)e.getSource(); // ... Button b=(Button)e.getSource(); Status.setText(command); if (command.equals("Start")) { b.setLabel("Next"); c.setstep("Start"); c.redraw(); } else { c.setstep(command); c.redraw(); } } public void update(Graphics g) { repaint(); } public void paint(Graphics g) { c.redraw(); } } /************* Mpaint.java **********************************************/ /* This is the class for running Fox's algorithm and display it. */ /*************************************************************************/ import java.awt.*; import java.net.*; import java.io.*; import java.awt.event.*; import java.applet.*; public class Mpaint extends Canvas { public Graphics g; public int curstep=0; public int[] Colorarray = {220,210,200,190,180,170,160,150,140,130,120,110,100,90,80,70}; public int pt = 700; public int tmp; public int cnum; public int test; public int i; public int j; public int f; public Font Lft = new Font("ITALIC", Font.PLAIN,35); public Font Sft = new Font("ITALIC", Font.PLAIN,15); public Font msgft = new Font("ITALIC",Font.ITALIC,20); /* array for values in each nodes */ public int[] arrayA = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; public int[] arrayB = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; public int[] arrayC = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; public int[] arraybufferA = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; public int[] arraybufferB = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; public Mpaint() { setBackground(Color.blue); setForeground(Color.yellow); } /* method for showing this step by step */ public void redraw(){ repaint(); } /* control steps */ public void setstep(String s) { if (s.equals("Start")) curstep = 1; else if (s.equals("Next")) curstep = curstep +1; else if (s.equals("Fast")) pt = pt - 100; else if (s.equals("Slow")) pt = pt + 100; } /* call each steps */ public void paint(Graphics g) { super.paint(g); if (curstep == 0 ) { init_show(g, 1); init_show(g, 2); printmsg(g,"Press the START key",""); large_show(g); } if (curstep == 1 ) { printmsg(g,"Randomize the matrix and matrix A and B are ","partitioned among 16 processors. "); init_show(g, 1); init_show(g, 2); large_show(g); init_matrix(g); large_show(g); } if (curstep == 2) { printmsg(g,"Broadcast the selected block of A"," "); init_show(g, 1 ); init_show(g, 2 ); large_show(g); commA(g,0); broadcast(g,0); large_show(g); } if (curstep == 3) { printmsg(g, "Multiply the block of A received with the resident", "block of B"); init_show(g, 1 ); init_show(g, 2 ); large_show(g); mult(g); large_show(g); } if (curstep == 4) { printmsg(g,"Broadcast the selected block of A"," "); init_show(g, 1 ); init_show(g, 2 ); large_show(g); commA(g,1); broadcast(g,1); large_show(g); } if (curstep == 5) { printmsg(g,"Send the block of B to the processor directly above", "it and receive a block of B from below it." ); init_show(g, 1 ); init_show(g, 2 ); large_show(g); commB(g,1); shift(g,1); large_show(g); } if (curstep == 6) { printmsg(g, "Multiply the block of A received with the resident", "block of B"); init_show(g, 1 ); init_show(g, 2 ); large_show(g); mult(g); large_show(g); } if (curstep == 7) { printmsg(g,"Broadcast the selected block of A"," "); init_show(g, 1 ); init_show(g, 2 ); large_show(g); commA(g,2); broadcast(g,2); large_show(g); } if (curstep == 8) { printmsg(g,"Send the block of B to the processor directly above", "it and receive a block of B from below it." ); init_show(g, 1 ); init_show(g, 2 ); large_show(g); commB(g,2); shift(g,2); large_show(g); } if (curstep == 9) { printmsg(g, "Multiply the block of A received with the resident", "block of B"); init_show(g, 1 ); init_show(g, 2 ); large_show(g); mult(g); large_show(g); } if (curstep == 10) { printmsg(g,"Broadcast the selected block of A"," "); init_show(g, 1 ); init_show(g, 2 ); large_show(g); commA(g,3); broadcast(g,3); large_show(g); } if (curstep == 11) { printmsg(g,"Send the block of B to the processor directly above", "it and receive a block of B from below it." ); init_show(g, 1 ); init_show(g, 2 ); large_show(g); commB(g,3); shift(g,3); large_show(g); } if (curstep == 12) { printmsg(g, "Multiply the block of A received with the resident", "block of B"); init_show(g, 1 ); init_show(g, 2 ); large_show(g); mult(g); large_show(g); } if (curstep == 13 ) { init_show(g, 1); init_show(g, 2); printmsg(g,"End of steps",""); large_show(g); pt = 700; curstep = 0; } } public void init_matrix(Graphics g) { for (int i = 0 ; i<64 ; i++){ arrayA[i] = (int) Math.floor(Math.random() * 9); arrayB[i] = (int) Math.floor(Math.random() * 9); arraybufferA[i] = arrayA[i]; arraybufferB[i] = arrayB[i]; arrayC[i] = 0; } } /* showing current nodes */ public void large_show(Graphics g) { for (j = 0; j < 4; j++) { for (i = 0; i < 4; i++){ cnum = Colorarray[(j*4)+i]; g.setColor(new Color( cnum, cnum, cnum )); g.fill3DRect (400+(i*120)+5, 30+(j*120)+5, 120, 120, true); g.setFont(Lft); g.setColor(new Color ( 0 , 255-cnum , 0 )); g.drawString("A", 410+(i*120)+5, 70+(j*120)+5); g.setFont(Sft); g.drawString(Integer.toString(j)+","+Integer.toString(i), 435+(i*120)+5,70+(j*120)+5); g.drawString(Integer.toString(arraybufferA[(j*16)+(i*2)+0])+",", 435+(i*120)+35,70+(j*120)-10); g.drawString(Integer.toString(arraybufferA[(j*16)+(i*2)+1]), 435+(i*120)+60,70+(j*120)-10); g.drawString(Integer.toString(arraybufferA[(j*16)+(i*2)+8])+",", 435+(i*120)+35,70+(j*120)+5); g.drawString(Integer.toString(arraybufferA[(j*16)+(i*2)+9]), 435+(i*120)+60,70+(j*120)+5); g.setFont(Lft); g.setColor(new Color (0, 0, 255-cnum)); g.drawString("B", 410+(i*120)+5, 100+(j*120)+5); g.setFont(Sft); g.drawString(Integer.toString(j)+","+Integer.toString(i),435+(i*120)+5,100+(j*120)+5); g.drawString(Integer.toString(arraybufferB[(j*16)+(i*2)+0])+",", 435+(i*120)+35,100+(j*120)-10); g.drawString(Integer.toString(arraybufferB[(j*16)+(i*2)+1]), 435+(i*120)+60,100+(j*120)-10); g.drawString(Integer.toString(arraybufferB[(j*16)+(i*2)+8])+",", 435+(i*120)+35,100+(j*120)+5); g.drawString(Integer.toString(arraybufferB[(j*16)+(i*2)+9]), 435+(i*120)+60,100+(j*120)+5); g.setFont(Lft); g.setColor(new Color (255-cnum,0,0)); g.drawString("C", 410+(i*120)+5, 130+(j*120)+5); g.setFont(Sft); g.drawString(Integer.toString(j)+","+Integer.toString(i),435+(i*120)+5,130+(j*120)+5); g.drawString(Integer.toString(arrayC[(j*16)+(i*2)+0])+",", 435+(i*120)+35,130+(j*120)-10); g.drawString(Integer.toString(arrayC[(j*16)+(i*2)+1]), 435+(i*120)+60,130+(j*120)-10); g.drawString(Integer.toString(arrayC[(j*16)+(i*2)+8])+",", 435+(i*120)+35,130+(j*120)+5); g.drawString(Integer.toString(arrayC[(j*16)+(i*2)+9]), 435+(i*120)+60,130+(j*120)+5); } } } /* showing frames */ public void init_show(Graphics g,int array_set ) { int loc=0; if (array_set == 1) loc = 0; if (array_set == 2) loc = 240; g.setColor(new Color ( 0x19,0x19,0x70 )); g.setFont(new Font("Serif",Font.BOLD,25)); g.drawString(" Communication Steps ", 35,26); g.drawString(" Example with 16 processors", 450,26); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++){ if (array_set == 1) g.setColor(new Color ( 0 , Colorarray[(i*4)+j] , 0 ) ); if (array_set == 2) g.setColor(new Color ( 0 , 0, Colorarray[(i*4)+j] ) ); g.fill3DRect (35+(j*60), 35+loc+(i*60), 60, 60, true); g.setFont(Lft); if (array_set == 1) { g.setColor(new Color ( 0, 255-Colorarray[(i*4)+j], 0 )); g.drawString("A", 42+(j*60)+5, 70+(i*60)+5); } if (array_set == 2) { g.setColor(new Color ( 0 , 0, 255 - Colorarray[(i*4)+j]) ); g.drawString("B", 42+(j*60)+5, 70+(i*60)+5+loc); } g.setFont(Sft); g.drawString(Integer.toString(i)+","+Integer.toString(j), 65+(j*60)+5, 70+(i*60)+5+loc); } } } /* broadcast the value of A matrix */ public void broadcast(Graphics g, int order) { for (int j = 0; j < 4; j++){ for (int i = 0; i < 4; i++){ arraybufferA[(j*16)+(i*2)+0] = arrayA[(j*16)+(((j*2)+0+(order*2))%8)]; arraybufferA[(j*16)+(i*2)+1] = arrayA[(j*16)+(((j*2)+1+(order*2))%8)]; arraybufferA[(j*16)+(i*2)+8] = arrayA[(j*16)+8+(((j*2)+0+(order*2))%8)]; arraybufferA[(j*16)+(i*2)+9] = arrayA[(j*16)+8+(((j*2)+1+(order*2))%8)]; } } } /* shift the value of B matrix */ public void shift(Graphics g, int order) { for (int j = 0; j < 4; j++){ for (int i = 0; i < 4; i++){ arraybufferB[(j*16)+(i*2)+0] = arrayB[((j*16)+(i*2)+0+(order*16))%64]; arraybufferB[(j*16)+(i*2)+1] = arrayB[((j*16)+(i*2)+1+(order*16))%64]; arraybufferB[(j*16)+(i*2)+8] = arrayB[((j*16)+(i*2)+8+(order*16))%64]; arraybufferB[(j*16)+(i*2)+9] = arrayB[((j*16)+(i*2)+9+(order*16))%64]; } } } /* calculate in each steps */ public void mult(Graphics g) { for (int j = 0; j < 4; j++){ for (int i = 0; i < 4; i++){ arrayC[(j*16)+(i*2)+0] = (arrayC[(j*16)+(i*2)+0]) + (arraybufferA[(j*16)+(i*2)+0]*arraybufferB[(j*16)+(i*2)+0]) + (arraybufferA[(j*16)+(i*2)+1]*arraybufferB[(j*16)+(i*2)+8]); arrayC[(j*16)+(i*2)+1] = (arrayC[(j*16)+(i*2)+1]) + (arraybufferA[(j*16)+(i*2)+0]*arraybufferB[(j*16)+(i*2)+1]) + (arraybufferA[(j*16)+(i*2)+1]*arraybufferB[(j*16)+(i*2)+9]); arrayC[(j*16)+(i*2)+8] = (arrayC[(j*16)+(i*2)+8]) + (arraybufferA[(j*16)+(i*2)+8]*arraybufferB[(j*16)+(i*2)+0]) + (arraybufferA[(j*16)+(i*2)+9]*arraybufferB[(j*16)+(i*2)+8]); arrayC[(j*16)+(i*2)+9] = (arrayC[(j*16)+(i*2)+9]) + (arraybufferA[(j*16)+(i*2)+8]*arraybufferB[(j*16)+(i*2)+1]) + (arraybufferA[(j*16)+(i*2)+9]*arraybufferB[(j*16)+(i*2)+9]); } } } /* make the pause between steps */ public void pause(Graphics g,int p_time) { int tmp; for (int i = 0; i < p_time; i++){ for (int j = 0; j < p_time; j++) { tmp = i + j; } } } /* printing message for instruction panel */ public void printmsg(Graphics g, String msg1,String msg2) { g.setColor(new Color(220,220,30)); g.fill3DRect (400+5, 40+480+5, 480, 100, true); g.setFont(msgft); g.setColor(Color.black); g.drawString(msg1, 400+5+10,70+480+5+5); g.drawString(msg2, 400+5+10,70+480+5+5+30); } /* display for communication panel*/ public void commA(Graphics g, int order) { int t; int flag = 1; for (int j = 0; j < 6 ; j++) { for (int i = 0; i < 4 ; i++ ){ g.setColor(new Color ( 0,Colorarray[(i*4)+((i+order)%4)], 0)); if ((j%2) == 0) g.fill3DRect (35+((i+order)%4)*60,35+(i*60),60,60,false); if ((j%2) == 1) g.fill3DRect (35+((i+order)%4)*60,35+(i*60),60,60,true); g.setColor(new Color (Colorarray[(i*4)+((i+order)%4)],0,0)); g.setFont(Lft); g.drawString("A", 47+((i+order)%4)*60, 75+(i*60)); g.setFont(Sft); g.drawString(Integer.toString(i)+","+Integer.toString((i+order)%4), 70+((i+order)%4)*60, 75+(i*60)); } pause(g,pt); } for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++){ g.setColor(new Color ( 0 , Colorarray[(i*4)+j] , 0 ) ); g.fill3DRect (35+(j*60), 35+(i*60), 60, 60, false); g.setFont(Lft); g.setColor(new Color ( 255-Colorarray[(i*4)+j], 0 , 0 )); g.drawString("A", 42+(j*60)+5, 70+(i*60)+5); g.setFont(Sft); g.drawString(Integer.toString(i)+","+Integer.toString((i+order)%4), 65+(j*60)+5, 70+(i*60)+5); } pause(g,pt); } } public void commB(Graphics g, int order) { for ( int k = 0; k<4; k++) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++){ g.setColor(new Color ( 0 , 0 , Colorarray[(i*4)+j]) ); if ( (k%2)==0) { if ((j%2)==0) g.fill3DRect (35+(j*60), 35+(i*60)+240, 60, 60, false); if ((j%2)==1) g.fill3DRect (35+(j*60), 35+(i*60)+240, 60, 60, true); } if ( (k%2)==1) { if ((j%2)==0) g.fill3DRect (35+(j*60), 35+(i*60)+240, 60, 60, true); if ((j%2)==1) g.fill3DRect (35+(j*60), 35+(i*60)+240, 60, 60, false); } g.setFont(Lft); g.setColor(new Color ( 255-Colorarray[(i*4)+j], 0 ,0)); g.drawString("B", 42+(j*60)+5, 70+(i*60)+5+240); g.setFont(Sft); g.drawString(Integer.toString(i)+","+Integer.toString(j), 65+(j*60)+5, 240+70+(i*60)+5); } } pause(g,pt); } for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++){ g.setColor(new Color ( 0 , 0, Colorarray[((i*4)+j+4+(order*4))%16]) ); g.fill3DRect (30+(j*60)+5, 30+240+(i*60)+5, 60, 60, true); g.setColor(new Color (255-Colorarray[((i*4)+j+4+(order*4))%16],0,0)); //255-Colorarray[((i*4)+j+4+(order*4))%16])); g.setFont(Lft); g.drawString("B", 42+(j*60)+5, 70+240+(i*60)+5); g.setFont(Sft); g.drawString(Integer.toString((i+1+order)%4)+","+Integer.toString(j),65+(j*60)+5,70+240+(i*60)+5); } } } }