我无法在C中使用约200x200 Lapack Visual Studio为更大的矩阵分配内存,这是我的代码 [英] I can't allocate memory for bigger matrices then ~200x200 Lapack Visual Studio in C Here is my code

查看:99
本文介绍了我无法在C中使用约200x200 Lapack Visual Studio为更大的矩阵分配内存,这是我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对求解矩阵的执行时间进行基准测试,我无法得到比200x200还要多的值,我应该大概达到1500x1500或接近该值.我在VS上运行它.

I am bench-marking the execution time of solving matrices , and i cant get more then ~200x200, i should go probably 1500x1500 or close to that. I am running this on VS .

#include <stdlib.h>
#include <stdio.h>
#include "lapacke.h"
#include "lapacke_config.h"
#include <time.h>

/* Auxiliary routines prototypes */
extern void print_matrix(lapack_complex_double *a, int m, int n);
extern void generate_matrix(lapack_complex_double * matrix, int w, int h);

/* Parameters */
#define N 179

/* Main program */
int main() {
    clock_t t;
    /* Locals */
    lapack_int n = N,info;
    lapack_int ipiv[N];
    lapack_complex_double a[N*N];
    lapack_complex_double b[N*N];

    FILE* fp1 = fopen("complexNumsLog.txt", "w");

    int w = 1, h = 1, h2 = 1, i = 0, j = 0; 


    for(i = 1; i <= N; i++){
        for(j = 1; j <= N; j++){
            w = i;
            h = i;
            h2 = j;

            generate_matrix(a, w, h);
            generate_matrix(b, w, h2);

            // print_matrix(a, w, h);
            // print_matrix(b, w, h2);

            // getchar(); 


            t = clock();
            info = LAPACKE_zgesv(LAPACK_ROW_MAJOR, w, h2, a, h, ipiv, b, h2);
            t = clock() - t;

            fprintf(fp1, "Matrix A: %3dx%3d ", w, h);
            fprintf(fp1, "Matrix B: %3dx%3d ", w, h2);
            fprintf(fp1, "%3d milliseconds %2.3f seconds\n",t,((float)t)/CLOCKS_PER_SEC);

            /* Check for the exact singularity */
            if( info > 0 ) {
                printf( "The diagonal element of the triangular factor of A,\n" );
                printf( "U(%i,%i) is zero, so that A is singular;\n", info, info );
                printf( "the solution could not be computed.\n" );
                getchar();
                exit( 1 );
            }
        }
        printf("%d\n", i);
    }
    getchar();
    exit( 0 );
} 

void print_matrix(lapack_complex_double* a, int m, int n) {
    int i, j;
    for( i = 0; i < m; i++ ) {
        for( j = 0; j < n; j++ )
            printf( " (%6.2f,%6.2f)", a[i*m+j].real, a[i*m+j].imag );
        printf( "\n" );
    }
    printf( "********************************************\n" );
}



void generate_matrix(lapack_complex_double * matrix, int w, int h){
    int i,j;
    double r;
    for(i = 0; i < w; i++){
        for(j = 0; j < h; j++){
            r = (rand()%1000 - 500)/100.0;
            matrix[i*w+j].real = r;
            r = (rand()%1000 - 500)/100.0;
            matrix[i*w+j].imag = r;;
        }
    }
}

推荐答案

您正在堆积自己的筹码.通常,您的程序仅分配了几MB的堆栈空间,并且您正在尝试在堆栈上分配所有数据.当您达到200x200以上时,您很快就会用完堆栈空间.

You're blowing your stack. Your program is typically only allocated a few MB of stack space, and you're trying to allocate all of your data on the stack. When you get above about 200x200, you very quickly run out of stack space.

要解决此问题,您需要在全局范围内或在堆上分配内存,其中大小仅受虚拟地址空间和/或总可用物理内存限制.由于大小是在编译时已知的,因此最容易在全局范围内分配大小:

To fix this, you need to either allocate the memory at global scope or on the heap, where the size is only limited by your virtual address space and/or total available physical memory. Since the size is known at compile time, it's easiest to allocate it at global scope:

/* Parameters */
#define N 179
lapack_int ipiv[N];
lapack_complex_double a[N*N];
lapack_complex_double b[N*N];    

/* Main program */
int main() {
    ...
}

这篇关于我无法在C中使用约200x200 Lapack Visual Studio为更大的矩阵分配内存,这是我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆