where is cula "culaSgesv" answer for X?

164 Views Asked by At

I just downloaded Cula and I want to use it's implemented functions for solving system of linear equation I looked into Examples Directory and I saw below code but it's very confusing when they want to obtain X solution of A*X=B they just copy B in X and since A is identity diagonal matrix so the answer IS, "B" and in this line of code nothing happens

status = culaSgesv(N, NRHS, A, N, IPIV, X, N);

(changing X to B didn't help!)

would you please tell me whats going on? Please tell me how can I get the answer "X" from this?

if anyone need any further information please just tell me.

#ifdef CULA_PREMIUM
void culaDoubleExample()
{
#ifdef NDEBUG
int N = 4096;
#else
int N = 780;
#endif
int NRHS = 1;
int i;

culaStatus status;

culaDouble* A = NULL;
culaDouble* B = NULL;
culaDouble* X = NULL;
culaInt* IPIV = NULL;

culaDouble one = 1.0;
culaDouble thresh = 1e-6;
culaDouble diff;

printf("-------------------\n");
printf("       DGESV\n");
printf("-------------------\n");

printf("Allocating Matrices\n");
A = (culaDouble*)malloc(N*N*sizeof(culaDouble));
B = (culaDouble*)malloc(N*sizeof(culaDouble));
X = (culaDouble*)malloc(N*sizeof(culaDouble));
IPIV = (culaInt*)malloc(N*sizeof(culaInt));
if(!A || !B || !IPIV)
    exit(EXIT_FAILURE);

printf("Initializing CULA\n");
status = culaInitialize();
checkStatus(status);

// Set A to the identity matrix
memset(A, 0, N*N*sizeof(culaDouble));
for(i = 0; i < N; ++i)
    A[i*N+i] = one;

// Set B to a random matrix (see note at top)
for(i = 0; i < N; ++i)
    B[i] = (culaDouble)rand();
memcpy(X, B, N*sizeof(culaDouble));

memset(IPIV, 0, N*sizeof(culaInt));

printf("Calling culaDgesv\n");
DWORD dw1 = GetTickCount();
status = culaDgesv(N, NRHS, A, N, IPIV, X, N);

DWORD dw2 = GetTickCount();
cout<<"Time difference is "<<(dw2-dw1)<<" milliSeconds"<<endl;
if(status == culaInsufficientComputeCapability)
{
    printf("No Double precision support available, skipping example\n");
    free(A);
    free(B);
    free(IPIV);
    culaShutdown();
    return;
}
checkStatus(status);

printf("Verifying Result\n");
for(i = 0; i < N; ++i)
{
    diff = X[i] - B[i];
    if(diff < 0.0)
        diff = -diff;
    if(diff > thresh)
        printf("Result check failed:  i=%d  X[i]=%f  B[i]=%f", i, X[i], B[i]);
}

printf("Shutting down CULA\n\n");
culaShutdown();

free(A);
free(B);
free(IPIV);
}
1

There are 1 best solutions below

0
Robert Crovella On BEST ANSWER

You mention Sgesv but the sample code you have shown is for Dgesv. Nevertheless, the answer is the same.

According to the Netlib LAPACK reference, the B matrix of RHS vectors is passed to the function as the 6th parameter:

[in,out] B

      B is DOUBLE PRECISION array, dimension (LDB,NRHS)
      On entry, the N-by-NRHS matrix of right hand side matrix B.
      On exit, if INFO = 0, the N-by-NRHS solution matrix X. 

And the X matrix is returned in the same parameter location. So B when passed to the function contains the NxNRHS right-hand-side vectors, and the same parameter returns the X result.

In the code you have shown, they are actually passing a variable called X and after the result is returned (in the same variable X) they are comparing it against a variable named B which is perhaps confusing, but the concept is the same.

Since the A matrix in the example is the identity matrix, the correct solution of Ax = b is x=b

For the general case, you should pass your B matrix of RHS vectors in the 6th parameter location. Upon completion of the function, the result (X) will be returned in the same parameter.