我正在用 C 语言编写一个程序,该程序使用线程将分区数组中的数字相加,该分区数组使用一些晦涩的命令行参数来确定应该使用多少个分区。
这是我在简报中苦苦挣扎的部分的副本:
Divide the array into 2**n partitions (n is another command-line argument) and create
one thread to do simple sum of all values stored in one partition of the array. Each
thread can calculate which locations of the array it should search given the thread index
(0,1, … 2**n-1). For example, if n=1 and the array is size 512, then thread 0 should sum
array locations [0,255] and thread 1 should search array locations [256-511]. Each
thread should store the partial sum it produces in a global array. Note that since the
threads do not share any data, they do not need to be synchronized, however you do need
to wait until the last thread completes processing before continuing on to the next
stage.
我完全不知道如何对数组进行分区以及为什么线程对于这项任务来说是必要的,但鉴于这是我的要求的一部分,我将不得不使用它们。如果有人可以帮助解释清楚一点,以及我将在哪个庄园中进行分区和线程的原因,我将不胜感激。我已经创建了数组并用随机数填充了它。