MapReduce: program of Matrix Multiplication

1.Matrix Multiplication 's basic principle:

(1)Sum the result of Matrix A's row multiplying Matrix B's column as the new Matrix.
(2)The Matrix A's row subscript become the new Matrix 's row subscript and The Matrix B's column subscript become the new Matrix 's column.

2.When we code , How do we think deeply the basic principle:

(1)The factor 's row subscript in Matrix A must be equal to the factor's column subscript in Matrix B.
(2)Matrix A 's all row factor must be multiply with Matrix B 's all column factor.

(3)In MapReduce Parallel programming framework, the same key value will be shuffle in a same list passing to Reduce.

4In Reduce, Sum the result of Matrix A's row multiplying Matrix B's column as the new Matrix, and  write the sum to output.

so, we need to design the arithmetic so as to achieve MatrixMultiplication.

(1)First ,we need to construt the key in map in order to shuffle Matrix A's row factor and Matrix B's columnfactor, which participate in the multiplication, in a same  reduce input value list.

(2)In the value list, we need another factor of the value in map to control suming the Multiplication 's result.

3.Design the arithmetic:

Pminj  = (A * B)minj = sum(Ami mj * Bmj nj);

In the Map,the Key and the Value be designed as this:

Matrix A:

key:[mi,  nj]

value:[A,  mj,  Ami mj ]

Matrix B:

key:[mi,  nj]

value:[A,  mj,  Ami mj ]

4.The Program :

package com.catchingsun.matrix;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

public class MatrixMultiply {

    private static final int columnN = 3;
    private static final int rowM = 5;
    private static final int columnM = 6;

    public static class MatrixMap extends Mapper<Object, Text, Text, Text > {
        private Text map_key = new Text();
        private Text map_value = new Text();

        private static int mmi = 0;
        private static int ni = 0;

        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            FileSplit fileSplit = (FileSplit) context.getInputSplit();
            String fileName = fileSplit.getPath().getName();
            int mj = 0;
            if (fileName.contains("M")) {
                mmi++;
                String[] tuple = value.toString().split(",");
                for (String s : tuple) {
                    mj++;
                    for (int k = 1; k < columnN + 1; k++) {
                        map_key.set(mmi + "," + k);
                        map_value.set("M" + "," + mj + "," + s);
                        context.write(map_key, map_value);
                    }
                }
            } else if (fileName.contains("N")) {
                ni++;
                int nj = 0;
                String[] tuple = value.toString().split(",");
                for (String s : tuple) {
                    nj++;
                    for (int i = 1; i < rowM + 1; i++) {
                        Text str = new Text();
                        map_key.set(i + "," + nj);
                        map_value.set("N" + "," + ni + "," + s);
                        context.write(map_key, map_value);
                    }
                }
            }
        }
    }

    public static class MatrixReduce extends Reducer<Text, Text, Text, Text> {

        private int sum = 0;
        private int M[] = new int[columnM + 1];
        private int N[] = new int[columnM + 1];

        public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException{
            for(Text val : values){
                String [] tuple = val.toString().split(",");
                if(tuple[0].equals("M")){
                    M[Integer.parseInt(tuple[1])] = Integer.parseInt(tuple[2]);
                }else{
                    N[Integer.parseInt(tuple[1])] = Integer.parseInt(tuple[2]);
                }
            }
            for(int i = 1; i < columnM + 1; i ++){
                sum += M[i] * N[i];
            }
            context.write(key, new Text(Integer.toString(sum)));
            sum = 0;
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();

        Job job = new Job(conf, "MatrixMultiply");
        job.setJarByClass(MatrixMultiply.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        job.setMapperClass(MatrixMultiply.MatrixMap.class);
        job.setReducerClass(MatrixMultiply.MatrixReduce.class);

        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        job.waitForCompletion(true);
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章