我正在考虑使用APARAPI实现Levenshtein距离算法的可能性,但我遇到了一些limitations设置的问题-特别是我需要在内核中创建一个被禁止的数组。
有没有办法绕过这个问题,或者更好的是,有没有人有一个与APARAPI一起工作的Levenshtein距离的方法?
附加的代码只是为了对APARAPI内容进行排序,我知道我没有对结果做任何事情,我现在只执行了一次。
Kernel kernel = new Kernel() {
@Override
public void run() {
ld("hello", "heya");
}
public int ld(String s, String t) {
int d[]; // matrix
int n; // length of s
int m; // length of t
int i; // iterates through s
int j; // iterates through t
int s_i; // ith character of s
int t_j; // jth character of t
int cost; // cost
// Step 1
n = s.length();
m = t.length();
if (n == 0) {
return m;
}
if (m == 0) {
return n;
}
int firstSize = n+1;
d = new int[firstSize*(m + 1)]; //THIS ISN'T ALLOWED
// Step 2
for (i = 0; i <= n; i++) {
d[firstSize*i+0] = i;
}
for (j = 0; j <= m; j++) {
d[firstSize*0+j] = j;
}
// Step 3
for (i = 1; i <= n; i++) {
s_i = s.charAt(i - 1);
// Step 4
for (j = 1; j <= m; j++) {
t_j = t.charAt(j - 1);
// Step 5
if (s_i == t_j) {
cost = 0;
} else {
cost = 1;
}
// Step 6
int a = d[firstSize*(i - 1)+j] + 1;
int b = d[firstSize*i+(j - 1)] + 1;
int c = d[firstSize*(i - 1)+(j - 1)] + cost;
int mi;
mi = a;
if (b < mi) {
mi = b;
}
if (c < mi) {
mi = c;
}
d[firstSize*i+j] = mi;
}
}
return d[firstSize*n+m];
}
};
kernel.execute(1);发布于 2012-02-08 00:27:39
正如您所指出的,Aparapi不允许在内核主体中使用任何形式的new,但是您可以预先分配内核代码可以访问的int缓冲区。
此外,因为您可以在运行时确定组的大小,所以您的缓冲区不必很大,而是Kernel.getGroupSize()的合理比例/比率。
当然,您需要将参数从String转换为char[],以满足aparapi的对象限制(不允许使用String),但我认为在aparapi讨论列表上的类似线程中,您已经找到了解决方法。
如果你准备尝试一些“实验代码”,我在SVN中有一个标记为SupportLocalMemory的分支,它允许你将临时int[]缓冲区放在本地内存中,这样性能也会更好。
加里
https://stackoverflow.com/questions/9164529
复制相似问题