- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在编写用于模拟的树算法。每个处理器都有自己的树。在程序的特定点,我必须检查特定树中是否有不属于那里的粒子。我收集它们并将它们发送到正确的树/处理器。
我的问题是关于我收集粒子并将它们放入动态大小列表的过程。由于我必须发送到另一棵树的粒子数量不是常数,所以我必须使用动态数组。
我实现了一个小程序,所有这一切都应该发生。但它只适用于小的 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;
}
}
问题可能出在这一行:plist->list[k] = realloc(plist->list[k], DIM * sizeof(plist->list[k]));
我收到以下错误:
*** Error in `./append_struct': realloc(): invalid next size: 0x00000000015df540 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7fc931b3e7e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x834aa)[0x7fc931b4a4aa]
/lib/x86_64-linux-gnu/libc.so.6(realloc+0x179)[0x7fc931b4b839]
./append_struct[0x400b5e]
./append_struct[0x4009bf]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fc931ae7830]
./append_struct[0x4007b9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:02 3670408 /home/exp/append_struct
00601000-00602000 r--p 00001000 08:02 3670408 /home/exp/append_struct
00602000-00603000 rw-p 00002000 08:02 3670408 /home/exp/append_struct
015df000-01600000 rw-p 00000000 00:00 0 [heap]
7fc92c000000-7fc92c021000 rw-p 00000000 00:00 0
7fc92c021000-7fc930000000 ---p 00000000 00:00 0
7fc9318b1000-7fc9318c7000 r-xp 00000000 08:02 4985364 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fc9318c7000-7fc931ac6000 ---p 00016000 08:02 4985364 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fc931ac6000-7fc931ac7000 rw-p 00015000 08:02 4985364 /lib/x86_64-linux-gnu/libgcc_s.so.1
7fc931ac7000-7fc931c87000 r-xp 00000000 08:02 4994073 /lib/x86_64-linux-gnu/libc-2.23.so
7fc931c87000-7fc931e87000 ---p 001c0000 08:02 4994073 /lib/x86_64-linux-gnu/libc-2.23.so
7fc931e87000-7fc931e8b000 r--p 001c0000 08:02 4994073 /lib/x86_64-linux-gnu/libc-2.23.so
7fc931e8b000-7fc931e8d000 rw-p 001c4000 08:02 4994073 /lib/x86_64-linux-gnu/libc-2.23.so
7fc931e8d000-7fc931e91000 rw-p 00000000 00:00 0
7fc931e91000-7fc931ea9000 r-xp 00000000 08:02 4994056 /lib/x86_64-linux-gnu/libpthread-2.23.so
7fc931ea9000-7fc9320a8000 ---p 00018000 08:02 4994056 /lib/x86_64-linux-gnu/libpthread-2.23.so
7fc9320a8000-7fc9320a9000 r--p 00017000 08:02 4994056 /lib/x86_64-linux-gnu/libpthread-2.23.so
7fc9320a9000-7fc9320aa000 rw-p 00018000 08:02 4994056 /lib/x86_64-linux-gnu/libpthread-2.23.so
7fc9320aa000-7fc9320ae000 rw-p 00000000 00:00 0
7fc9320ae000-7fc9320d4000 r-xp 00000000 08:02 4994051 /lib/x86_64-linux-gnu/ld-2.23.so
7fc9322b5000-7fc9322b8000 rw-p 00000000 00:00 0
7fc9322d0000-7fc9322d3000 rw-p 00000000 00:00 0
7fc9322d3000-7fc9322d4000 r--p 00025000 08:02 4994051 /lib/x86_64-linux-gnu/ld-2.23.so
7fc9322d4000-7fc9322d5000 rw-p 00026000 08:02 4994051 /lib/x86_64-linux-gnu/ld-2.23.so
7fc9322d5000-7fc9322d6000 rw-p 00000000 00:00 0
7ffc92bdb000-7ffc92bfc000 rw-p 00000000 00:00 0 [stack]
7ffc92bfc000-7ffc92bfe000 r--p 00000000 00:00 0 [vvar]
7ffc92bfe000-7ffc92c00000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted (core dumped)
编辑:
我的示例代码只是一个粗略的草图,我认为自己是 C 的初学者。这可能是我的问题不是很清楚的原因。在我的实际代码中,我在每个处理器上用我的粒子(2D 中的四叉树和 3D 中的八叉树)构建树结构。每个处理器都有其他粒子。我在递归树遍历中使用它们在树中的位置来识别错误的粒子,并将它们发送到其他处理器,因为我想要紧凑的树结构。为了做到这一点,我必须将错误的粒子放入一个列表中,然后我可以将该列表传递给 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]);
}
// ...
}
请注意,此实现不需要在执行期间调用任何 realloc()。
一旦您的应用使用“普通”线程工作,使其与 MPI 兼容就会简单得多。
关于c - 树算法中内存的动态分配和重新分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44824468/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!