我在 C# 中执行以下循环的线程时遇到问题:
for (int i = 1; i < matrix.scoreMatrix.GetLength(0); i++)
{
for (int j = 1; j < matrix.scoreMatrix.GetLength(1); j++)
{
matrix.CalculateScore(i, j);
}
}
此循环将匹配数组填充到 Smith Waterman 算法。这需要很多时间,因为我想改进填充矩阵的过程。
必须从左上角填充矩阵,因为以下单元格是根据位于上方和左侧的单元格计算的。
我的想法是利用这 2-3 个额外的线程来填充每个线阵列,如下图所示:
任何提示或类似的安排都会非常有帮助。
我做了这样的事情:
主功能:
int i = 0, t1_row=0, t2_row=0, t3_row=0, finished_lines=0;
Thread t1 = new Thread(() => getnext1(matrix, i, t1_row, t2_row, t3_row, finished_lines));
Thread t2 = new Thread(() => getnext2(matrix, i, t1_row, t2_row, t3_row, finished_lines));
Thread t3 = new Thread(() => getnext3(matrix, i, t1_row, t2_row, t3_row, finished_lines));
t1.Start();
t2.Start();
t3.Start();
t1.Join();
t2.Join();
t3.Join();
线程函数:
public static void getnext1(SWMatrix matrix, int i, int t1_row, int t2_row, int t3_row, int finished_lines)
{
do
{
for (int j = 1; j < matrix.scoreMatrix.GetLength(1); j++)
{
if (t1_row <= t3_row - 1 || finished_lines >= i - 2)
{
matrix.CalculateScore(i, j);
t1_row++;
}
else
{
j--;
}
}
finished_lines++;
i++;
t1_row = 0;
}
while (i >= matrix.scoreMatrix.GetLength(0));
}
public static void getnext2(SWMatrix matrix, int i, int t1_row, int t2_row, int t3_row, int finished_lines)
{
do
{
for (int j = 1; j < matrix.scoreMatrix.GetLength(1); j++)
{
if (t2_row <= t1_row - 1 || finished_lines >= i - 2)
{
matrix.CalculateScore(i, j);
t2_row++;
}
else
{
j--;
}
}
finished_lines++;
i++;
t2_row = 0;
}
while (i >= matrix.scoreMatrix.GetLength(0));
}
public static void getnext3(SWMatrix matrix, int i, int t1_row, int t2_row, int t3_row, int finished_lines)
{
do
{
for (int j = 1; j < matrix.scoreMatrix.GetLength(1); j++)
{
if (t3_row <= t2_row - 1 || finished_lines >= i - 2)
{
matrix.CalculateScore(i, j);
t3_row++;
}
else
{
j--;
}
}
finished_lines++;
i++;
t3_row = 0;
}
while (i >= matrix.scoreMatrix.GetLength(0));
}
查询执行时间几乎延长了两倍。但我也有线程工作的信息。如何优化这段代码?有什么建议吗?我在一台有 4 个处理器的机器上测试它。