使用多线程在java中生成矩阵以利用超级计算机的所有CPU核心

时间:2017-08-09 09:56:44

标签: java multithreading matrix

我正在处理一个处理大量数据和计算的问题也是为了所有这个目的我们有一台处理能力为270 T FLOPS的超级计算机所以我们的数据基本上是矩阵形式所以我们决定划分生成通过使用线程将矩阵分成几个部分所以问题只有我们如何才能在我们的函数中实现这样的事情我们只是使用参数来划分任务但是线程的运行函数没有参数

static int start=0,end;
int row=10000;
int col =10000,count=0,m=2203,n=401;
double p=Math.PI,t=Math.sqrt(3),pi=(p/(col+1));
double mul,a,b,c,d,e,f,xx,yy;
int[][] matrix = new int [row][col];


private void sin1()
{
    // TODO Auto-generated method 
    for (int i =start; i < row; i++)
    {
        for (int j = 0; j <col; j++) 
        {
            xx=((i+1)*pi);
            yy=((j+1)*pi);
            a=Math.cos(((2*m)-n)*((2*(xx))/3));
            b=Math.sin(((2*(n*(yy)))/t));
            c=Math.cos(((((2*n)-m)*(2*(xx)))/3));
            d=Math.sin(((2*m)*(yy)/t));
            e=Math.cos((((m+n)*(2*(xx)))/3));
            f=Math.sin((((m-n)*(2*(yy)))/t));

               mul=(a*b)-(c*d)+(e*f);

            if(mul<0)
            {
                matrix[i][j]=0;
            }
            else
            {
                matrix[i][j]=1;
            }
        System.out.print(matrix[i][j]);
        }
        System.out.println();
    }

} 

我们首先测试它的1000万个值

1 个答案:

答案 0 :(得分:0)

代码清楚地表明您缺乏任何形式的Java编程知识。如果你想为超级计算机编写代码,这是一件坏事。 Java幸运地拥有一套很好的工具来解决各种问题,但是你需要知道在哪种情况下使用哪些工具。

在你的情况下你可以i.E.使用并行流来跨核心传播这一代:

static final int start = 0;
static int end;
static final int row = 10000;
static final int col = 10000, count = 0, m = 2203, n = 401;
static final double t = Math.sqrt(3);
static final double pi = (Math.PI / (col + 1));

final int[][] matrix = new int[row][col];

public int generateMatrixEntry(final int i, final int j) {
  final double xx = ((i + 1) * pi);
  final double yy = ((j + 1) * pi);
  final double a = Math.cos(((2 * m) - n) * ((2 * (xx)) / 3));
  final double b = Math.sin(((2 * (n * (yy))) / t));
  final double c = Math.cos(((((2 * n) - m) * (2 * (xx))) / 3));
  final double d = Math.sin(((2 * m) * (yy) / t));
  final double e = Math.cos((((m + n) * (2 * (xx))) / 3));
  final double f = Math.sin((((m - n) * (2 * (yy))) / t));

  final double mul = (a * b) - (c * d) + (e * f);
  return (mul < 0) ? 0 : 1;
}

private void sin1() {
  IntStream.range(start, row).parallel().forEach((i) -> {
    for (int j = 0; j < col; j++) {
      matrix[i][j] = generateMatrixEntry(i, j);
    }
  });
}

然而,这只是一种可能适合或可能不适合您的硬件的可能解决方案。如果以上方法无法解决您的问题,您绝对需要具有更深入Java知识的人为您选择合适的工具。

相关问题