QR decomposition used cula

General discussion for CULA. Use this forum for questions, examples, feedback, and feature requests.

QR decomposition used cula

Postby kangsia » Tue Oct 11, 2011 1:55 am

I used example in the Basic cula.
Example name is deviceInterface.

I used array A as follows.

A[0] = 1; A[1] = 1; A[2] = 1; A[3] = 1;
A[4] = -1; A[5] = 4; A[6] = 4; A[7] = -1;
A[8] = 4; A[9] = -2; A[10] = 2; A[11] = 0; <--- column major format.

Matlab function using...
[Q,R] = qr(A) is..
Q = -0.5, 0.5, -0.5, -0.5;
-0.5, -0.5, 0.5, -0.5;
-0.5, -0.5, -0.5, 0.5;
-0.5, 0.5, 0.5, 0.5;

R = -2, -3, -2;
0, -5, 2;
0, 0, -4;
0, 0, 0;

The culaDeviceSgeqrf function using...
Output
A = -2, 0.333, 0.333;
0.333, -3, -5;
0.4, -0.2, -2;
2, -4, -0.5;

TAU = 1.5, 1.66667, 1.6

Where is the Q and R??

Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>

#include <culapackdevice.h>

#include <cuda_runtime.h>
#ifdef _MSC_VER
#   pragma comment(lib, "cudart.lib")
#endif

using namespace std;

void checkStatus(culaStatus status)
{
    char buf[80];

    if(!status)
        return;

    culaGetErrorInfoString(status, culaGetErrorInfo(), buf, sizeof(buf));
    printf("%s\n", buf);

    culaShutdown();
    exit(EXIT_FAILURE);
}


void checkCudaError(cudaError_t err)
{
    if(!err)
        return;

    printf("%s\n", cudaGetErrorString(err));

    culaShutdown();
    exit(EXIT_FAILURE);
}


int main(int argc, char** argv)
{
#ifdef NDEBUG
    int M = 8192;
#else
    int M = 4;
#endif
    int N = 3;

    cudaError_t err;
    culaStatus status;

    // point to host memory
    float* A = NULL;
    float* TAU = NULL;

    // point to device memory
    float* Ad = NULL;
    float* TAUd = NULL;

    printf("Allocating Matrices\n");
    A = (float*)malloc(M*N*sizeof(float));
    TAU = (float*)malloc(N*sizeof(float));
    if(!A || !TAU)
        exit(EXIT_FAILURE);

   A[0] =  1; A[1] =  1; A[2] =  1; A[3] = 1;
   A[4] = -1; A[5] =  4; A[6] =  4; A[7] =-1;
   A[8] =  4; A[9] =  -2; A[10] = 2; A[11] = 0;

    err = cudaMalloc((void**)&Ad, M*N*sizeof(float));
    checkCudaError(err);

    err = cudaMalloc((void**)&TAUd, N*sizeof(float));
    checkCudaError(err);

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

    //memset(A, 0, M*N*sizeof(float));
    err = cudaMemcpy(Ad, A, M*N*sizeof(float), cudaMemcpyHostToDevice);
    checkCudaError(err);

    printf("Calling culaDeviceSgeqrf\n");
    status = culaDeviceSgeqrf(M, N, Ad, M, TAUd);
    checkStatus(status);

    err = cudaMemcpy(A, Ad, M*N*sizeof(float), cudaMemcpyDeviceToHost);
    checkCudaError(err);
    err = cudaMemcpy(TAU, TAUd, N*sizeof(float), cudaMemcpyDeviceToHost);
    checkCudaError(err);

   for ( size_t i(0); i < 4; i++){
      for ( size_t j(0); j < 3; j++){
         cout << A[i*3+j] << " ";
      }
      cout << endl;
   }

   for ( size_t i(0); i < 3; i++){
      cout << TAU[i] << " ";
   }
    printf("Shutting down CULA\n");
    culaShutdown();

    cudaFree(Ad);
    cudaFree(TAUd);
    free(A);
    free(TAU);

    return EXIT_SUCCESS;
}
kangsia
 
Posts: 1
Joined: Sun Sep 25, 2011 7:17 pm

Re: QR decomposition used cula

Postby john » Wed Oct 19, 2011 9:30 am

The R is present as the upper triangle of that matrix. The Q is formed by the routine culaSorgqr or by any other LAPACK's SORGQR.
john
Administrator
 
Posts: 587
Joined: Thu Jul 23, 2009 2:31 pm


Return to General CULA Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron