并行preFIX总和 - 最快的实现 [英] Parallel prefix sum - fastest Implementation
问题描述
我想要实现使用C ++并行preFIX和算法。我的程序应输入数组 X [1 .... N]
,它应该显示阵列 Y [N]输出
。 (注N的最大值为1000。)
I want to implement the parallel prefix sum algorithm using C++. My program should take the input array x[1....N]
,and it should display the output in the array y[N]
. (Note the maximum value of N is 1000.)
到目前为止,我经历了许多研究论文,甚至在算法中维基百科。 但我的节目也应显示每个步骤的输出,步骤,也是操作/指令。
So far, I went through many research papers and even the algorithm in Wikipedia. But my program should also display the output, the steps and also the operations/instructions of each step.
欲最快的实现像我希望尽量减少操作以及步骤的数目。
I want the fastest implementation like I want to minimise the number of operations as well as the steps.
例如::
x = {1, 2, 3, 4, 5, 6, 7, 8 } - Input
y = ( 1, 3, 6, 10, 15, 21, 28, 36) - Output
但是伴随着显示在y数组作为输出,我的节目也应显示每个步骤的操作。我也参照这个线程计算preFIX总和,但可以从它那里得到多少帮助
But along with displaying the y array as output, my program should also display the operations of each step. I also refer this thread calculate prefix sum ,but could get much help from it.
推荐答案
下面这段code将做的工作。
Following piece of code will do the job
void prfxSum()
{
int *x=0;
int *y=0;
int sum=0;
int num=0;
int i=0;
cout << "Enter the no. of elements in input array : ";
cin >> num;
x = new int[num];
y = new int[num];
while(i < num)
{
cout << "Enter element " << i+1 << " : ";
cin >> x[i++];
}
cout << "Output array :- " << endl;
i = 0;
while(i < num)
{
sum += x[i];
y[i] = sum;
cout << y[i++] << ", ";
}
delete [] x;
delete [] y;
}
以下是在执行输出
Following is the output on execution
Enter the no. of elements in input array : 8
Enter element 1 : 1
Enter element 2 : 2
Enter element 3 : 3
Enter element 4 : 4
Enter element 5 : 5
Enter element 6 : 6
Enter element 7 : 7
Enter element 8 : 8
Output array :-
1, 3, 6, 10, 15, 21, 28, 36
您可以通过它从文件喂左右避免数组x []的1000元的用户输入。
You can avoid the user input of 1000 elements of array x[] by feeding it from file or so.
这篇关于并行preFIX总和 - 最快的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!