小编Lew*_*wis的帖子

在处理大型numpy数组时,Python会随机降低到0%的CPU使用率,导致代码"挂断"?

我一直在运行一些代码,其中一部分从二进制文件加载一个大的1D numpy数组,然后使用numpy.where()方法更改数组.

以下是代码中执行的操作的示例:

import numpy as np
num = 2048
threshold = 0.5

with open(file, 'rb') as f:
    arr = np.fromfile(f, dtype=np.float32, count=num**3)
    arr *= threshold

arr = np.where(arr >= 1.0, 1.0, arr)
vol_avg = np.sum(arr)/(num**3)

# both arr and vol_avg needed later
Run Code Online (Sandbox Code Playgroud)

我已经运行了很多次(在免费的机器上,即没有其他禁止CPU或内存使用)没有问题.但最近我注意到,有时候代码会挂起很长一段时间,使运行时间长一个数量级.在这些情况下,我一直在监视%CPU和内存使用率(使用gnome系统监视器),并发现python的CPU使用率降至0%.

在上述操作之间使用基本打印来调试,似乎是任意的,哪个操作导致暂停(即open(),np.fromfile(),np.where()各自分别导致随机运行挂起).这就好像我被随机扼杀,因为在其他运行中没有挂起.

我已经考虑过垃圾收集或这个问题,但是我看不出与我的问题有任何明显的关系(例如击键没有效果).

进一步说明:二进制文件为32GB,机器(运行Linux)有256GB内存.我通过ssh会话远程运行此代码.

编辑:这可能是偶然的,但我注意到,如果我刚刚重启机器后运行代码,就没有挂断.似乎它们在几次运行后开始发生,或者至少是系统的其他用途.

python arrays numpy

6
推荐指数
1
解决办法
1222
查看次数

了解可以存储在C中的浮点数中的最大值

我遇到了一些float我不理解的C类型的行为,希望可以解释一下.使用float.hI中定义的宏可以确定数据类型可以在给定硬件上存储的最大/最小值.但是,当执行不应超过这些限制的计算时,我发现类型float变量在double成功的地方失败.以下是一个最小的例子,它在我的机器上编译.

#include <stdio.h>
#include <stdlib.h>
#include <float.h>

int main(int argc, char **argv)
{
    int gridsize;
    long gridsize3;

    float *datagrid;

    float sumval_f;
    double sumval_d;

    long i;

    gridsize = 512;
    gridsize3 = (long)gridsize*gridsize*gridsize;

    datagrid = calloc(gridsize3, sizeof(float));
    if(datagrid == NULL)
    {
        free(datagrid);
        printf("Memory allocation failed\n");
        exit(0);
    }

    for(i=0; i<gridsize3; i++)
    {
        datagrid[i] += 1.0;
    }

    sumval_f = 0.0;
    sumval_d = 0.0;
    for(i=0; i<gridsize3; i++)
    {
        sumval_f += datagrid[i];
        sumval_d += (double)datagrid[i];
    }

    printf("\ngridsize3 = …
Run Code Online (Sandbox Code Playgroud)

c floating-point

3
推荐指数
1
解决办法
300
查看次数

标签 统计

arrays ×1

c ×1

floating-point ×1

numpy ×1

python ×1