- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在运行在 http://www.norstad.org/matrix-multiply/index.html 找到的 MapReduce 矩阵乘法程序.我发现当输入矩阵中有 0 时,此实现无法正常工作。但我不明白为什么,我该如何修改程序才能使其工作?MapReduce 操作完成,但输出始终是一个所有元素为 0 的矩阵。
我的输入矩阵 A 和 B 是:
Matrix A Matrix B
0 0 0 6 7 4
0 1 6 9 1 3
7 8 9 7 6 2
输出矩阵:
0 0 0
0 0 0
0 0 0
以下内容取自作业的日志文件:
matrixB 的 map 输出:
##### Map setup: matrixA = false for hdfs://localhost/user/hadoop-user/B
strategy = 4
R1 = 4
I = 3
K = 3
J = 3
IB = 3
KB = 3
JB = 3
##### Map input: (0,0) 6
##### Map output: (0,0,0,1) (0,0,6)
##### Map input: (0,1) 7
##### Map output: (0,0,0,1) (0,1,7)
##### Map input: (0,2) 4
##### Map output: (0,0,0,1) (0,2,4)
##### Map input: (1,0) 9
##### Map output: (0,0,0,1) (1,0,9)
##### Map input: (1,1) 1
##### Map output: (0,0,0,1) (1,1,1)
##### Map input: (1,2) 3
##### Map output: (0,0,0,1) (1,2,3)
##### Map input: (2,0) 7
##### Map output: (0,0,0,1) (2,0,7)
##### Map input: (2,1) 6
##### Map output: (0,0,0,1) (2,1,6)
##### Map input: (2,2) 2
##### Map output: (0,0,0,1) (2,2,2)
矩阵 A 的映射输出:
##### Map setup: matrixA = true for hdfs://localhost/user/hadoop-user/A
strategy = 4
R1 = 4
I = 3
K = 3
J = 3
IB = 3
KB = 3
JB = 3
##### Map input: (1,1) 1
##### Map output: (0,0,0,0) (1,1,1)
##### Map input: (1,2) 6
##### Map output: (0,0,0,0) (1,2,6)
##### Map input: (2,0) 7
##### Map output: (0,0,0,0) (2,0,7)
##### Map input: (2,1) 8
##### Map output: (0,0,0,0) (2,1,8)
##### Map input: (2,2) 9
##### Map output: (0,0,0,0) (2,2,9)
请注意,矩阵 A 的映射不会从输入文件中读取零。注意:两个输入文件都作为序列文件存储在 HDFS 中,输出也是一个序列文件。有人可以阐明问题所在吗?谢谢!
Mapper类的代码如下:
/** The job 1 mapper class. */
private static class Job1Mapper
extends Mapper<IndexPair, IntWritable, Key, Value>
{
private Path path;
private boolean matrixA;
private Key key = new Key();
private Value value = new Value();
public void setup (Context context) {
init(context);
FileSplit split = (FileSplit)context.getInputSplit();
path = split.getPath();
matrixA = path.toString().startsWith(inputPathA);
if (DEBUG) {
System.out.println("##### Map setup: matrixA = " + matrixA + " for " + path);
System.out.println(" strategy = " + strategy);
System.out.println(" R1 = " + R1);
System.out.println(" I = " + I);
System.out.println(" K = " + K);
System.out.println(" J = " + J);
System.out.println(" IB = " + IB);
System.out.println(" KB = " + KB);
System.out.println(" JB = " + JB);
}
}
private void printMapInput (IndexPair indexPair, IntWritable el) {
System.out.println("##### Map input: (" + indexPair.index1 + "," +
indexPair.index2 + ") " + el.get());
}
private void printMapOutput (Key key, Value value) {
System.out.println("##### Map output: (" + key.index1 + "," +
key.index2 + "," + key.index3 + "," + key.m + ") (" +
value.index1 + "," + value.index2 + "," + value.v + ") ");
}
private void badIndex (int index, int dim, String msg) {
System.err.println("Invalid " + msg + " in " + path + ": " + index + " " + dim);
System.exit(1);
}
public void map (IndexPair indexPair, IntWritable el, Context context)
throws IOException, InterruptedException
{
if (DEBUG) printMapInput(indexPair, el);
int i = 0;
int k = 0;
int j = 0;
if (matrixA) {
i = indexPair.index1;
if (i < 0 || i >= I) badIndex(i, I, "A row index");
k = indexPair.index2;
if (k < 0 || k >= K) badIndex(k, K, "A column index");
} else {
k = indexPair.index1;
if (k < 0 || k >= K) badIndex(k, K, "B row index");
j = indexPair.index2;
if (j < 0 || j >= J) badIndex(j, J, "B column index");
}
value.v = el.get();
if (matrixA) {
key.index1 = i/IB;
key.index3 = k/KB;
key.m = 0;
value.index1 = i % IB;
value.index2 = k % KB;
for (int jb = 0; jb < NJB; jb++) {
key.index2 = jb;
context.write(key, value);
if (DEBUG) printMapOutput(key, value);
}
} else {
key.index2 = j/JB;
key.index3 = k/KB;
key.m = 1;
value.index1 = k % KB;
value.index2 = j % JB;
for (int ib = 0; ib < NIB; ib++) {
key.index1 = ib;
context.write(key, value);
if (DEBUG) printMapOutput(key, value);
}
}
}
}
如果有任何语法错误,那只是因为我复制粘贴了代码并在此处进行了修改,所以我可能忽略了一些东西。
我需要有关 Map 函数逻辑的帮助,它不会从输入文件中读取 0,谁能告诉我为什么?
最佳答案
在 TestMatrixMultiply.java 中,从您链接的网站,它可能包含您用来将矩阵编码为预期的 IndexPair-backed 文件格式的代码,有函数 writeMatrix:
public static void writeMatrix (int[][] matrix, int rowDim, int colDim, String pathStr)
throws IOException
{
Path path = new Path(pathStr);
SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf, path,
MatrixMultiply.IndexPair.class, IntWritable.class,
SequenceFile.CompressionType.NONE);
MatrixMultiply.IndexPair indexPair = new MatrixMultiply.IndexPair();
IntWritable el = new IntWritable();
for (int i = 0; i < rowDim; i++) {
for (int j = 0; j < colDim; j++) {
int v = matrix[i][j];
if (v != 0) { // !!! well, that would be why we aren't writing 0s
indexPair.index1 = i;
indexPair.index2 = j;
el.set(v);
writer.append(indexPair, el);
}
}
}
writer.close();
}
注释插入到内部 for
循环的第二行。
您的映射器未读取任何 0,因为您的输入文件不包含任何 0。
该代码经过精心设计,假定所有缺失值均为 0,并且它会执行额外的检查以避免发出 0,以尝试最大程度地减少网络流量。
下面的大部分内容都是错误的,因为我误解了算法
(虽然上面的部分仍然有用)
在链接页面中,您正在使用策略 3。策略 3 依赖于分区程序行为和排序顺序。不幸的是,分区程序是错误的!这与未打印出来的 0 是分开的。分区程序在这里完全错误,你得到的矩阵全是 0,因为它乘以之前初始化为 0 的数据,并且从未被 block 的正确数据覆盖。这在 1 机操作中是隐藏的,因为分区程序是空操作,但在大多数集群中都会中断。
The partitioner maps intermediate key (kb, jb, ib) to a reducer r as follows:
r = (jb*KB + kb) mod R
需要保证同一个block的所有key都分配给同一个reducer。不幸的是,它保证这不会发生,除非 KB % numReducers == 0
:
map (key, value)
if from matrix A with key=(i,k) and value=a(i,k)
for 0 <= jb < NJB
emit (k/KB, jb, i/IB), (i mod IB, k mod KB, a(k,j)) // compare this...
if from matrix B with key=(k,j) and value=b(k,j)
emit (k/KB, j/JB, -1), (k mod KB, j mod KB, b(k,j)) // ...to this
对于矩阵 A,正在迭代键 jb。对于矩阵 B,正在计算 key jb。由于分配给 reducer 是循环的,这保证 A 矩阵键不会分配给与 B 矩阵键相同的 reducer。因此,该算法失败了,因为它假设了一些关于键的分配和排序的东西,这些东西可以证明是不正确的。当且仅当存在一个分配了所有键的 reducer 时,键顺序是正确的,但分区器是错误的!
分区器必须修改为使用策略 3 的 kb % numReducers
。这不是一个很好的分区器,但它是唯一可以工作的分区器,因为需要不相同的键以特定顺序排序到同一个 reducer 。
您实际放入问题中的代码与错误实际存在的位置无关。
关于java - Hadoop 矩阵乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9708427/
如果矩阵A在X中,矩阵B在Y中。 进行乘法运算只是 Z = X*Y。正确假设两个数组的大小相同。 如何使用 for 循环计算它? 最佳答案 ja72 的anwser 是错误的,请查看我在其下的评论以了
我有一个 C 程序,它有 n 次乘法(单次乘法和 n 次迭代),我发现另一个逻辑有 n/2 次迭代(1 次乘法 + 2 次加法)。我知道两者都是 O(n) 的复杂性。但就 CPU 周期而言。哪个更快?
我有一个矩阵x: x <- matrix(1:8, nrow = 2, ncol = 4, byrow = 2) # [,1] [,2] [,3] [,4] #[1,] 1 2 3
我有一个矩阵x: x <- matrix(1:8, nrow = 2, ncol = 4, byrow = 2) # [,1] [,2] [,3] [,4] #[1,] 1 2 3
我正在创建一个基于电影 InTime 的 Minecraft 插件,并尝试创建代码,在玩家死亡时玩家将失去 25% 的时间。 当前代码是: String minus = itapi.getTimeSt
我正在尝试将 2 个矩阵与重载的 * 运算符相乘并打印结果。虽然看起来我不能为重载函数提供超过 1 个参数。如何将这两个矩阵传递给重载函数?请在下面查看我的实现。 #include #include
为什么在 Java 中使用 .*?例如 double probability = 1.*count/numdata; 给出相同的输出: double probability = count/numda
如果我尝试将两个值与单位相乘,则会出现意外错误。 $test: 10px; .testing{ width: $test * $test; } result: 100px*px isn't a v
我正在尝试计算库存中所有产品的总值(value)。表中的每种产品都有价格和数量。因此,我需要将每种产品的价格乘以数量,然后将所有这些加在一起以获得所有产品的总计。根据上一个问题,我现在可以使用 MyS
我正在尝试计算库存中所有产品的总值(value)。表中的每种产品都有价格和数量。因此,我需要将每种产品的价格乘以数量,然后将所有这些加在一起以获得所有产品的总计。根据上一个问题,我现在可以使用 MyS
大家好,我有以下代码行 solution first = mylist.remove((int)(Math.random() * mylist)); 这给了我一个错误说明 The operator *
我必须做很多乘法运算。如果我考虑效率,那么我应该使用位运算而不是常规的 * 运算吗?如果有差异如何进行位运算?提前致谢.. 最佳答案 不,您应该使用乘法运算符,让优化编译器决定如何最快地完成它。 您会
两个 n 位数字 A 和 B 的乘法可以理解为移位的总和: (A << i1) + (A << i2) + ... 其中 i1, i2, ... 是 B 中设置为 1 的位数。 现在让我们用 OR
我想使用 cuda 6 进行 bool 乘法,但我无法以正确的方式做到这一点。B 是一个 bool 对称矩阵,我必须进行 B^n bool 乘法。 我的 C++ 代码是: for (m=0; m
我正在编写一个定点类,但遇到了一些问题...乘法、除法部分,我不确定如何模拟。我对部门运算符(operator)进行了非常粗暴的尝试,但我确信这是错误的。到目前为止,它是这样的: class Fixe
我有TABLE_A我需要创建 TABLE_A_FINAL 规则: 在TABLE_A_FINAL中我们有包含 ID_C 的所有可能组合的行如果在 TABLE_A与 ID_C 的组合相同我们乘以 WEIG
这个问题在这里已经有了答案: Simple way to repeat a string (32 个答案) 关闭 6 年前。 我有一个任务是重复字符乘以它例如用户应该写重复输入 3 R 输出的字母和
我最近学习了C++的基础知识。我发现了一些我不明白的东西。这是让我有点困惑的程序。 #include using namespace std; int main()
我有两个列表: list_a = list_b = list(范围(2, 6)) final_list = [] 我想知道如何将两个列表中的所有值相乘。我希望我的 final_list 包含 [2*2
如何修改此代码以适用于任何基数? (二进制、十六进制、基数 10 等) int mult(int a, int b, int base){ if((a<=base)||(b<=base)){
我是一名优秀的程序员,十分优秀!