使用MapReduce实现矩阵向量相乘

37 篇文章 0 订阅
1 篇文章 0 订阅

1  描述

     假定有一个 n*n 的矩阵 M ,其第 i 行第 j 列的元素记为  。假定有一个 n 维向量 v ,其第 j 个元素记为  。于是,矩阵 M 和向量 v 的乘积结果是一个 n 维向量 x,其第 i 个元素  为 
 
     如:



    要求输入:

11 22 33
33 44 55
66 77 88

    输出:

0	220
1	418
2	715

2  实现思路

    假如这里 n 很大,但还没有大到向量 v 不足以放入内存的地步。将矩阵 M 存放在一个文件中,向量 v 作为常量数组放在程序中。那么我们便可以从矩阵元素在文件中的位置确定该元素的行列下标。同样, v 向量的元素  ,可以通过数组下标获取该元素的行列下标。

Map 函数:

    对矩阵元素  , Map 任务会产生键值对( i,   )。因此,计算  的所有 n 个求和项   的键值都相同。

Reduce 函数:

      Reduce 任务将所有与给定键 i 关联的值相加即可得到( i ,  )。

逻辑图:



 

3  代码实现

public class MatrixVectorCompute {

  public static class TokenizerMapper extends
      Mapper<Object, Text, Text, IntWritable> {

    private Text lineNumber = new Text(); // 矩阵行序号
    private static int i = 0;
    private final static int[] vector = {2, 3, 4}; // 向量值

    public void map(Object key, Text value, Context context)
        throws IOException, InterruptedException {
      StringTokenizer itr = new StringTokenizer(value.toString());
      int j = 0; // 向量序号
      lineNumber.set(i + "");
      while (itr.hasMoreTokens()) {
        int result = vector[j] * Integer.parseInt(itr.nextToken());
        IntWritable one = new IntWritable(result);
        context.write(lineNumber, one);
        j ++;
      }
      i ++;
    }
  }

  public static class IntSumReducer extends
      Reducer<Text, IntWritable, Text, IntWritable> {
    private IntWritable result = new IntWritable();

    public void reduce(Text key, Iterable<IntWritable> values,
        Context context) throws IOException, InterruptedException {
      int sum = 0;
      for (IntWritable val : values) {
        sum += val.get();
      }
      result.set(sum);
      context.write(key, result);
    }
  }

  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    
    Job job = new Job(conf, "word count11");
    job.setJarByClass(MatrixVectorCompute.class);
    
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    
    FileInputFormat.addInputPath(job, new Path("input"));
    FileOutputFormat.setOutputPath(job, new Path("output"));
    
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

4  总结 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值