0

我有两个列表,我需要将一个中的元素与另一个进行匹配,并将这些元素输出到一个新的矩阵(输出)中。Fortran 中最快的方法是什么?到目前为止的蛮力:

do i = 1,Nlistone
   do j = 1,Nlisttwo
     if A(i).eq.B(j) then
        output(i) = B(j)
     end if
   end do
end do

openmp版本:

!$OMP PARALLEL PRIVATE(i,j)
do i = 1,NA
   do j = 1,NB
     if A(i).eq.B(j) then
        filtered(i) = A(j)
     end if
   end do
end do
!$OMP END PARALLEL DO

肯定有更好的方法可以做到这一点,并且在这里排序不是一个选项抱歉(+向量元素没有任何特定的顺序)。是否有类似于maskpython 的布尔参数?

4

2 回答 2

2

ANY使用类似这样的内在函数可能会更快

do i = 1,Nlistone
     if (any(B==A(i))) output(i) = A(i)
end do

但我不会打赌这种改进的性能,我会测试两个版本。您应该能够安全地将其包装在!$OMP PARALLEL DO构造中,因为 的每个元素output仅由一个线程写入。

于 2013-01-24T22:55:59.440 回答
0

You can also include the condition testing in a forall or (newer) do concurrent construct, which both use the same syntax:

do concurrent(i = 1:Nlistone, any(a(i) == b)) 
  output(i) = a(i)
end do

This might be faster than your code listing, depending on the nature A and B. But it still has the same time complexity of O(n^2), while sorting can be O(n*log(n)). If Nlistone /= Nlisttwo, you could gain a factor equal to the ratio of these sizes, by only looping through the shorter array and matching its elements to the longer one.

于 2013-01-25T15:36:11.737 回答