C 树算法中内存的动态分配和再分配
我正在为模拟编写一个树算法。每个处理器都有自己的树。在程序中的某个特定点,我必须检查特定树中是否有不属于该树的粒子。我收集它们并将它们发送到正确的树/处理器 我的问题是关于收集粒子并将它们放入动态大小列表的过程。因为我必须发送到另一棵树的粒子数量不是恒定的,所以我必须使用动态数组 我实施了一个小程序,所有这些都应该发生。但它只适用于较小的C 树算法中内存的动态分配和再分配,c,memory-management,malloc,realloc,C,Memory Management,Malloc,Realloc,我正在为模拟编写一个树算法。每个处理器都有自己的树。在程序中的某个特定点,我必须检查特定树中是否有不属于该树的粒子。我收集它们并将它们发送到正确的树/处理器 我的问题是关于收集粒子并将它们放入动态大小列表的过程。因为我必须发送到另一棵树的粒子数量不是恒定的,所以我必须使用动态数组 我实施了一个小程序,所有这些都应该发生。但它只适用于较小的N。但是对于小的N有时也会出现错误。重新分配过程可能不起作用 #include <stdio.h> #include <stdlib.h>
N
。但是对于小的N
有时也会出现错误。重新分配过程可能不起作用
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DIM 2
// Struct for particles
typedef struct {
double m;
double x[DIM];
int id;
} Particle;
// Structs for lists I want to fill with particle data
typedef struct {
double **list; // every processor has its own list
int *counter; // length of the list
} ParticleList;
void generateParticles(Particle *p, int N);
void buildList(Particle *p, ParticleList *plist, int numprocs, int N);
int main() {
time_t t;
srand((unsigned)time(&t));
// Generate and print data
int N = 3;
Particle *p = (Particle*)malloc(N * sizeof(*p));
generateParticles(p, N);
for (int i = 0; i < N; i++) {
printf("id: %d m: %lf x: %lf %lf\n", p[i].id, p[i].m, p[i].x[0], p[i].x[1]);
}
// Fill lists
int numprocs = 4;
ParticleList plist;
plist.list = malloc(sizeof(double*) * numprocs);
// At the beginning every list should be of size zero
// Therefore I initialize lists for every processor of size zero
for (int k = 0; k < numprocs; k++)
plist.list[k] = malloc(sizeof(double) * 0);
plist.counter = calloc(numprocs, sizeof(int));
// Fill the lists randomly
buildList(p, &plist, numprocs, N);
for (int k = 0; k < numprocs; k++) {
printf("%d\n", plist.counter[k]);
for (int c = 0; c < (DIM * plist.counter[k]); c++) {
printf("%lf ", plist.list[k][c]);
}
printf("\n");
}
free(p);
return 0;
}
void buildList(Particle *p, ParticleList *plist, int numprocs, int N) {
for (int k = 0; k < numprocs; k++) {
for (int i = 0; i < N; i++) {
if (rand() % 10 < 3) { // randomly choose particles to fill the list
plist->counter[k]++;
// Here might be the problem?
plist->list[k] = realloc(plist->list[k], DIM * sizeof(plist->list[k]));
for (int j = plist->counter[k]; j < (plist->counter[k] + DIM); j++)
plist->list[k][j] = p[i].x[j];
}
}
}
}
void generateParticles(Particle *p, int N) {
for (int i = 0; i < N; i++) {
for (int d = 0; d < DIM; d++) {
p[i].x[d] = rand() % 10;
}
p[i].m = rand() % 10;
p[i].id = i;
}
}
编辑:
我的示例代码只是一个粗略的草图,我认为自己是C中的初学者。这可能是我的问题不太清楚的原因。在我的实际代码中,我在每个处理器上用我的粒子(二维四叉树和三维八叉树)构建一个树结构。每个处理器都有其他粒子。我在递归树漫游中使用错误的粒子在树中的位置来识别它们,并将它们发送给其他处理器,因为我想要紧凑的树结构。为了做到这一点,我必须将错误的粒子放入一个列表中,然后将其传递到MPI库,以便将数据发送到其他处理器。粒子的数量通常比处理器的数量大得多(N>>numProc)。我不太明白你的推理,它可能会优化计算速度,但你需要有一个工作程序来考虑这样做 此外,将粒子分发到处理器的算法也不起作用。如前所述,粒子有70%的几率不会被分配到任何列表中 在粒子列表的声明中:
typedef struct {
double **list; // a list of double* ? It's going to be hard to find which double
// belongs to which Particle, this makes your app more confusing
// and much harder to write than it ought to.
int *counter; // a length of lengths? what's its length?
} ParticleList;
你应该考虑这样做。这可能会更好,我省略了对calloc()结果的错误检查,您应该经常这样做。我选择了calloc(),因为它会清除分配的内存
typedef struct {
struct Particle* next;
double m;
double x[DIM];
int id;
} Particle;
typedef struct Particle* ParticleList;
// this should look familiar to you.
void insert_particle(ParticleList* list, struct Particle* part)
{
part->next = NULL; // just to make sure we don't introduce bugs here
if (*list == NULL)
{
*list = part;
return;
}
struct Particle* p = *list;
while (p)
{
if (p->next == NULL)
{
p->next = part
return;
}
p = p->next;
}
}
int main()
{
int i;
int numProcs = 4;
int assigned_proc;
int N = 3; // less particles than threads? This does not sound right...
// ...
// allocate empty lists and all particles.
ParticleList* procparts = calloc(numprocs, sizeof(ParticleList));
struct Particle* particles = calloc(N, sizeof(struct Particle));
for (i = 0; i < N; ++i) // for each particle ...
{
// initialize particle location...
particles[i].id = i;
// ... and x[]...
assigned_proc = rand() % numprocs; // pick a processor...
insert_particle(&procparts[assigned_proc], &particles[i]);
}
// ...
}
typedef结构{
结构粒子*下一步;
双m;
双x[DIM];
int-id;
}粒子;
类型定义结构粒子*粒子列表;
//这对你来说应该很熟悉。
空心插入粒子(粒子列表*列表,结构粒子*零件)
{
part->next=NULL;//只是为了确保我们不会在这里引入bug
如果(*list==NULL)
{
*列表=零件;
返回;
}
结构粒子*p=*列表;
while(p)
{
如果(p->next==NULL)
{
p->next=零件
返回;
}
p=p->next;
}
}
int main()
{
int i;
int numProcs=4;
int赋值_proc;
int N=3;//粒子数少于线程数?听起来不对。。。
// ...
//分配空列表和所有粒子。
ParticleList*procparts=calloc(numprocs,sizeof(ParticleList));
结构粒子*particles=calloc(N,sizeof(结构粒子));
对于(i=0;i
请注意,此实现在执行期间不需要调用realloc()
一旦你的应用程序使用“普通”线程工作,使其与MPI兼容就简单多了。“不工作”不是一个有用的问题描述。清楚明确地说明您观察到的错误或症状。请定义“不起作用”。你的程序编译吗?如果没有,您会收到什么错误消息?它跑吗?示例输出和实际输出是什么?等等?为什么,我们没有更多的5分钟来更正编辑?我的错。@kaylum我改进了我的问题。这里
plist->list[k][j]=p[I]。x[j]
j上升到plist->counter[k]+DIM-1
,但只为DIM*sizeof(plist->list[k])
保留了空间sizeof(plist->list[k])
无论如何都是错误的,应该是sizeof(plist->list[k][0])
afaics。我更新了我的问题以使我的问题更清楚。通常N
比numProcs
大得多。这只是一个例子。我不明白函数中发生了什么insert\u particle()
。为了使用MPI\u Send()
和MPI\u Receive()
我想我需要每个处理器的粒子阵列和该阵列的长度。我错了吗?不,你错了。但到了切换的时候,你怎么才能找到哪个双粒子属于哪个粒子呢?我不明白你的最后一个问题。你能给我解释一下你看得更清楚的问题吗?在你最初的问题中,你说你必须定期在MPI作业之间切换粒子数据。此时,您必须重建浮点数组。在MPI缓冲区中,如果不跟踪它们所属的粒子,就不能只填充两倍。簿记是项目中最具挑战性的方面。
typedef struct {
struct Particle* next;
double m;
double x[DIM];
int id;
} Particle;
typedef struct Particle* ParticleList;
// this should look familiar to you.
void insert_particle(ParticleList* list, struct Particle* part)
{
part->next = NULL; // just to make sure we don't introduce bugs here
if (*list == NULL)
{
*list = part;
return;
}
struct Particle* p = *list;
while (p)
{
if (p->next == NULL)
{
p->next = part
return;
}
p = p->next;
}
}
int main()
{
int i;
int numProcs = 4;
int assigned_proc;
int N = 3; // less particles than threads? This does not sound right...
// ...
// allocate empty lists and all particles.
ParticleList* procparts = calloc(numprocs, sizeof(ParticleList));
struct Particle* particles = calloc(N, sizeof(struct Particle));
for (i = 0; i < N; ++i) // for each particle ...
{
// initialize particle location...
particles[i].id = i;
// ... and x[]...
assigned_proc = rand() % numprocs; // pick a processor...
insert_particle(&procparts[assigned_proc], &particles[i]);
}
// ...
}