使用 aparapi 计算编辑距离
我正在研究使用 APARAPI 实现 Levenshtein 距离算法的可能性,但我遇到了 限制 - 具体来说,我需要在内核中创建一个被禁止的数组。
有没有办法解决这个问题,或者更好的是有人有一种与 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);
I'm looking at the possibility of implementing a Levenshtein distance algorithm using APARAPI, but I'm running into some problems with the limitations posed - specifically that I need to create an array in the kernel which is prohibited.
Is there a way around this, or better has anyone got a method for Levenshtein distance that works with APARAPI?
The attached code is just in place to try to sort the APARAPI stuff out, I know that I'm not doing anything with the result and I'm just executing once at the moment.
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所指出的,Aparapi 不允许在内核主体中出现任何形式的 new ,但是您可以预先分配内核代码可以访问的整数缓冲区。
此外,因为您可以在运行时确定组大小,所以您的缓冲区不必很大,但 Kernel.getGroupSize() 的合理比例/比率即可。
当然,您需要将参数从 String 转换为 char[] 以满足 Aparapi 的对象限制(不允许使用字符串),但我认为从 aparapi 讨论列表上的类似线程中您已经找到了解决方法。
如果您准备尝试一些“实验代码”,我在 SVN 中有一个标记为 SupportLocalMemory 的分支,它允许您将临时 int[] 缓冲区放置在本地内存中,这也应该具有更高的性能。
加里
As you indicate Aparapi does not allow any form of new in the Kernel body, however you could pre-allocate a buffer of ints which the Kernel code can access.
Furthermore because you can determine the group size at runtime your buffer does not have to be huge, but some reasonable proportion/ratio of Kernel.getGroupSize().
Of course you will need to convert the arguments from String to char[] to satisfy Aparapi's Object restriction (Strings are not allowed), but I think from a similar thread on aparapi discussion lists you had already found a workaround for that.
If you are prepared to experiment with some 'experimental code' I have a branch in SVN tagged SupportLocalMemory which will allow you to place your temporary int[] buffer in local memory which should also be more performant.
Gary