我从10到21(总是顺序)给出一组元素, 我生成了相同大小的数组,其中大小是运行时确定的。
3个生成的数组的示例(数组#是动态的以及所有数组中的元素数,其中一些元素可以是0 - 未使用):
A1 = [10,11,12,13]
A2 = [14,15,16,17]
A3 = [18,19,20,21]
这些生成的数组将被赋予不同的进程,以对元素进行一些计算。我的目标是平衡每个获得阵列的进程的负载。我的意思是:
以给定的例子,有
A1 = 46
A2 = 62
A3 = 78
对每个线程给出的元素的潜在迭代。
我想重新排列初始数组,为每个进程提供相同数量的工作,例如:
A1 = [21,11,12,13] = 57
A2 = [14,15,16,17] = 62
A3 = [18,19,20,10] = 67
(不是平等分配,但比初始更公平)。分布可以是不同的,只要它们接近某个最佳分布并且优于第一个和最后一个数组的最差(初始)情况。 正如我所看到的,使用不同的索引可以实现不同的分布[在这里进行数组拆分{可能不均匀}]
这适用于给定的例子,但可能有奇怪的情况..
所以,我认为这是一个反射问题(由于缺乏正确定义的知识),其中应该通过对角线看到数组,例如:
10 | 111213
1415 | 1617
181920 | 21
然后可以进行明显的替换..
我尝试实现如下:
if(rest == 0)
payload_size = (upper-lower)/(processes-1);
else
payload_size = (upper-lower)/(processes-1) + 1;
//printf("payload size: %d\n", payload_size);
long payload[payload_size];
int m = 0;
int k = payload_size/2;
int added = 0; //track what been added so far (to skip over already added elements)
int added2 = 0; // same as 'added'
int p = 0;
for (i = lower; i <= upper; i=i+payload_size){
for(j = i; j<(i+payload_size); j++){
if(j <= upper){
if((j-i) > k){
if(added2 > j){
added = j;
payload[(j-i)] = j;
printf("1 adding data: %d at location: %d\n", payload[(j-i)], (j-i));
}else{
printf("else..\n");
}
}else{
if(added < upper - (m+1)){
payload[(j-i)] = upper - (p*payload_size) - (m++);
added2 = payload[(j-i)];
printf("2 adding data: %d at location: %d\n", payload[(j-i)], (j-i));
}else{
payload[(j-i)] = j;
printf("2.5 adding data: %d at location: %d\n", payload[(j-i)], (j-i));
}
}
}else{ payload[(j-i)] = '\0'; }
}
p++;
k=k/2;
//printf("send to proc: %d\n", ((i)/payload_size)%(processes-1)+1);
}
..但是非常失败。
你肯定可以在实现中看到问题,因为它的可扩展性很差,不完整,杂乱,写得不好等等,以及开启,等等...
因此,在给定描述的情况下,我需要帮助实现或者想要更好的方法来实现我想要实现的目标。
P.S。我需要解决方案尽可能“ in-liney ”(避免循环嵌套) - 这就是我使用大量标志和全局索引的原因。
当然,这可以通过额外的循环和不必要的迭代来完成。我邀请那些可以和欣赏数组索引技术的人。
我确信在某个地方有一个解决方案,但我无法通过合适的Google查询来查找它。
提示?我想到使用索引%size_of_my_data来完成这个任务..
P.S。申请:described here
答案 0 :(得分:0)
通过简单的分配序列,您可以依次迭代地将min和max元素添加到每个列表中。有一些终止细节需要解决,但这是一般的想法。应用于您的示例,输出将如下所示:
john-schultzs-macbook-pro:~ jschultz$ ./a.out
10 21 13 18 = 62
11 20 14 17 = 62
12 19 15 16 = 62
当num_procs均匀地划分num_elems时,这样的简单反射分配将是最佳的。当它没有:
时,它将是次优的,但仍然是不错的#include <stdio.h>
int compute_dist(int lower, int upper, int num_procs)
{
if (lower > upper || num_procs <= 0)
return -1;
int num_elems = upper - lower + 1;
int num_elems_per_proc_floor = num_elems / num_procs;
int num_elems_per_proc_ceil = num_elems_per_proc_floor + (num_elems % num_procs != 0);
int procs[num_procs][num_elems_per_proc_ceil];
int i, j, sum;
// assign pairs of (lower, upper) to each process until we can't anymore
for (i = 0; i + 2 <= num_elems_per_proc_floor; i += 2)
for (j = 0; j < num_procs; ++j)
{
procs[j][i] = lower++;
procs[j][i+1] = upper--;
}
// handle left overs similarly to the above
// NOTE: actually you could use just this loop alone if you set i = 0 here, but the above loop is more understandable
for (; i < num_elems_per_proc_ceil; ++i)
for (j = 0; j < num_procs; ++j)
if (lower <= upper)
procs[j][i] = ((0 == i % 2) ? lower++ : upper--);
else
procs[j][i] = 0;
// print assignment results
for (j = 0; j < num_procs; ++j)
{
for (i = 0, sum = 0; i < num_elems_per_proc_ceil; ++i)
{
printf("%d ", procs[j][i]);
sum += procs[j][i];
}
printf(" = %d\n", sum);
}
return 0;
}
int main()
{
compute_dist(10, 21, 3);
return 0;
}
答案 1 :(得分:0)
这是我使用deque编写的O(n)解决方案(双端队列,不需要deque,可以使用简单的数组,但是deque因为popRight和popLeft而使代码干净)。代码是Python,而不是伪代码,但它应该很容易理解(因为它是Python)。:
def balancingSumProblem(seqStart = None, seqStop = None, numberOfArrays = None):
from random import randint
from collections import deque
seq = deque(xrange(seqStart or randint(1, 10),
seqStop and seqStop + 1 or randint(11,30)))
arrays = [[] for _ in xrange(numberOfArrays or randint(1,6))]
print "# of elements: {}".format(len(seq))
print "# of arrays: {}".format(len(arrays))
averageNumElements = float(len(seq)) / len(arrays)
print "average number of elements per array: {}".format(averageNumElements)
oddIteration = True
try:
while seq:
for array in arrays:
if len(array) < averageNumElements and oddIteration:
array.append(seq.pop()) # pop() is like popright()
elif len(array) < averageNumElements:
array.append(seq.popleft())
oddIteration = not oddIteration
except IndexError:
pass
print arrays
print [sum(array) for array in arrays]
balancingSumProblem(10,21,3) # Given Example
print "\n---------\n"
balancingSumProblem() # Randomized Test
基本上,从迭代到迭代,它在抓取大元素并在数组中均匀分布它们并抓取小元素并在数组中均匀分布之间交替。它从out到in(虽然你可以从in到out)并试图使用每个数组的平均元素数来进一步平衡它。
所有测试都不是100%准确,但在大多数随机测试中它都做得很好。您可以尝试在此处运行代码:http://repl.it/cJg
答案 2 :(得分:0)
我使用过这个实现,我在this report中提到过(实现适用于我用于测试(1-15K)(1-30K)和(1-100K)数据集的情况。我不是说它对所有情况都有效):
int aFunction(long lower, long upper, int payload_size, int processes)
{
long result, i, j;
MPI_Status status;
long payload[payload_size];
int m = 0;
int k = (payload_size/2)+(payload_size%2)+1;
int lastAdded1 = 0;
int lastAdded2 = 0;
int p = 0;
int substituted = 0;
int allowUpdate = 1;
int s;
int times = 1;
int times2 = 0;
for (i = lower; i <= upper; i=i+payload_size){
for(j = i; j<(i+payload_size); j++){
if(j <= upper){
if(k != 0){
if((j-i) >= k){
payload[(j-i)] = j- (m);
lastAdded2 = payload[(j-i)];
}else{
payload[(j-i)] = upper - (p*payload_size) - (m++) + (p*payload_size);
if(allowUpdate){
lastAdded1 = payload[(j-i)];
allowUpdate = 0;
}
}
}else{
int n;
int from = lastAdded1 > lastAdded2 ? lastAdded2 : lastAdded1;
from = from + 1;
int to = lastAdded1 > lastAdded2 ? lastAdded1 : lastAdded2;
int tempFrom = (to-from)/payload_size + ((to-from)%payload_size>0 ? 1 : 0);
for(s = 0; s < tempFrom; s++){
int restIndex = -1;
for(n = from; n < from+payload_size; n++){
restIndex = restIndex + 1;
payload[restIndex] = '\0';
if(n < to && n >= from){
payload[restIndex] = n;
}else{
payload[restIndex] = '\0';
}
}
from = from + payload_size;
}
return 0;
}
}else{ payload[(j-i)] = '\0'; }
}
p++;
k=(k/2)+(k%2)+1;
allowUpdate = 1;
}
return 0;
}