Click here to Skip to main content
15,881,812 members
Articles / Desktop Programming / Win32

Stopwatch

Rate me:
Please Sign up or sign in to vote.
4.97/5 (29 votes)
3 Jan 2015CPOL6 min read 66K   1.5K   43  
Benchmark C++ std::vector vs raw arrays, move assignable/constructable & copy assignable/constructable
#include "stdafx.h"

/*! @file sreadrb.c
 * \brief Read a matrix stored in Rutherford-Boeing format
 *
 * <pre>
 * -- SuperLU routine (version 4.0) --
 * Lawrence Berkeley National Laboratory.
 * June 30, 2009
 * </pre>
 *
 * Purpose
 * =======
 *
 * Read a FLOAT PRECISION matrix stored in Rutherford-Boeing format 
 * as described below.
 *
 * Line 1 (A72, A8)
 *      Col. 1 - 72   Title (TITLE)
 *      Col. 73 - 80  Matrix name / identifier (MTRXID)
 *
 * Line 2 (I14, 3(1X, I13))
 *      Col. 1 - 14   Total number of lines excluding header (TOTCRD)
 *      Col. 16 - 28  Number of lines for pointers (PTRCRD)
 *      Col. 30 - 42  Number of lines for row (or variable) indices (INDCRD)
 *      Col. 44 - 56  Number of lines for numerical values (VALCRD)
 *
 * Line 3 (A3, 11X, 4(1X, I13))
 *      Col. 1 - 3    Matrix type (see below) (MXTYPE)
 *      Col. 15 - 28  Compressed Column: Number of rows (NROW)
 *                    Elemental: Largest integer used to index variable (MVAR)
 *      Col. 30 - 42  Compressed Column: Number of columns (NCOL)
 *                    Elemental: Number of element matrices (NELT)
 *      Col. 44 - 56  Compressed Column: Number of entries (NNZERO)
 *                    Elemental: Number of variable indeces (NVARIX)
 *      Col. 58 - 70  Compressed Column: Unused, explicitly zero
 *                    Elemental: Number of elemental matrix entries (NELTVL)
 *
 * Line 4 (2A16, A20)
 *      Col. 1 - 16   Fortran format for pointers (PTRFMT)
 *      Col. 17 - 32  Fortran format for row (or variable) indices (INDFMT)
 *      Col. 33 - 52  Fortran format for numerical values of coefficient matrix
 *                    (VALFMT)
 *                    (blank in the case of matrix patterns)
 *
 * The three character type field on line 3 describes the matrix type.
 * The following table lists the permitted values for each of the three
 * characters. As an example of the type field, RSA denotes that the matrix
 * is real, symmetric, and assembled.
 *
 * First Character:
 *      R Real matrix
 *      C Complex matrix
 *      I integer matrix
 *      P Pattern only (no numerical values supplied)
 *      Q Pattern only (numerical values supplied in associated auxiliary value
 *        file)
 *
 * Second Character:
 *      S Symmetric
 *      U Unsymmetric
 *      H Hermitian
 *      Z Skew symmetric
 *      R Rectangular
 *
 * Third Character:
 *      A Compressed column form
 *      E Elemental form
 *
 * </pre>
 */

#include "hnum_pssp_defs.h"


namespace harlinn
{
    namespace numerics
    {
        namespace SuperLU
        {
            namespace Single
            {


                /*! \brief Eat up the rest of the current line */
                static int sDumpLine(FILE *fp)
                {
                    register int c;
                    while ((c = fgetc(fp)) != '\n') ;
                    return 0;
                }

                static int sParseIntFormat(char *buf, int *num, int *size)
                {
                    char *tmp;

                    tmp = buf;
                    while (*tmp++ != '(') ;
                    sscanf(tmp, "%d", num);
                    while (*tmp != 'I' && *tmp != 'i') ++tmp;
                    ++tmp;
                    sscanf(tmp, "%d", size);
                    return 0;
                }

                static int sParseFloatFormat(char *buf, int *num, int *size)
                {
                    char *tmp, *period;

                    tmp = buf;
                    while (*tmp++ != '(') ;
                    *num = atoi(tmp); /*sscanf(tmp, "%d", num);*/
                    while (*tmp != 'E' && *tmp != 'e' && *tmp != 'D' && *tmp != 'd'
                           && *tmp != 'F' && *tmp != 'f') {
                        /* May find kP before nE/nD/nF, like (1P6F13.6). In this case the
                           num picked up refers to P, which should be skipped. */
                        if (*tmp=='p' || *tmp=='P') {
                           ++tmp;
                           *num = atoi(tmp); /*sscanf(tmp, "%d", num);*/
                        } else {
                           ++tmp;
                        }
                    }
                    ++tmp;
                    period = tmp;
                    while (*period != '.' && *period != ')') ++period ;
                    *period = '\0';
                    *size = atoi(tmp); /*sscanf(tmp, "%2d", size);*/

                    return 0;
                }

                static int ReadVector(FILE *fp, int n, int *where, int perline, int persize)
                {
                    register int i, j, item;
                    char tmp, buf[100];

                    i = 0;
                    while (i < n) {
                        fgets(buf, 100, fp);    /* read a line at a time */
                        for (j=0; j<perline && i<n; j++) {
                            tmp = buf[(j+1)*persize];     /* save the char at that place */
                            buf[(j+1)*persize] = 0;       /* null terminate */
                            item = atoi(&buf[j*persize]); 
                            buf[(j+1)*persize] = tmp;     /* recover the char at that place */
                            where[i++] = item - 1;
                        }
                    }

                    return 0;
                }

                static int sReadValues(FILE *fp, int n, float *destination, int perline,
                        int persize)
                {
                    register int i, j, k, s;
                    char tmp, buf[100];

                    i = 0;
                    while (i < n) {
                        fgets(buf, 100, fp);    /* read a line at a time */
                        for (j=0; j<perline && i<n; j++) {
                            tmp = buf[(j+1)*persize];     /* save the char at that place */
                            buf[(j+1)*persize] = 0;       /* null terminate */
                            s = j*persize;
                            for (k = 0; k < persize; ++k) /* No D_ format in C */
                                if ( buf[s+k] == 'D' || buf[s+k] == 'd' ) buf[s+k] = 'E';
                            destination[i++] = atof(&buf[s]);
                            buf[(j+1)*persize] = tmp;     /* recover the char at that place */
                        }
                    }

                    return 0;
                }



                void
                sreadrb(int *nrow, int *ncol, int *nonz,
                        float **nzval, int **rowind, int **colptr)
                {

                    register int i, numer_lines = 0;
                    int tmp, colnum, colsize, rownum, rowsize, valnum, valsize;
                    char buf[100], type[4];
                    FILE *fp;

                    fp = stdin;

                    /* Line 1 */
                    fgets(buf, 100, fp);
                    fputs(buf, stdout);

                    /* Line 2 */
                    for (i=0; i<4; i++) {
                        fscanf(fp, "%14c", buf); buf[14] = 0;
                        sscanf(buf, "%d", &tmp);
                        if (i == 3) numer_lines = tmp;
                    }
                    sDumpLine(fp);

                    /* Line 3 */
                    fscanf(fp, "%3c", type);
                    fscanf(fp, "%11c", buf); /* pad */
                    type[3] = 0;
                #ifdef DEBUG
                    printf("Matrix type %s\n", type);
                #endif

                    fscanf(fp, "%14c", buf); sscanf(buf, "%d", nrow);
                    fscanf(fp, "%14c", buf); sscanf(buf, "%d", ncol);
                    fscanf(fp, "%14c", buf); sscanf(buf, "%d", nonz);
                    fscanf(fp, "%14c", buf); sscanf(buf, "%d", &tmp);

                    if (tmp != 0)
                        printf("This is not an assembled matrix!\n");
                    if (*nrow != *ncol)
                        printf("Matrix is not square.\n");
                    sDumpLine(fp);

                    /* Allocate storage for the three arrays ( nzval, rowind, colptr ) */
                    sallocateA(*ncol, *nonz, nzval, rowind, colptr);

                    /* Line 4: format statement */
                    fscanf(fp, "%16c", buf);
                    sParseIntFormat(buf, &colnum, &colsize);
                    fscanf(fp, "%16c", buf);
                    sParseIntFormat(buf, &rownum, &rowsize);
                    fscanf(fp, "%20c", buf);
                    sParseFloatFormat(buf, &valnum, &valsize);
                    sDumpLine(fp);

                #ifdef DEBUG
                    printf("%d rows, %d nonzeros\n", *nrow, *nonz);
                    printf("colnum %d, colsize %d\n", colnum, colsize);
                    printf("rownum %d, rowsize %d\n", rownum, rowsize);
                    printf("valnum %d, valsize %d\n", valnum, valsize);
                #endif

                    ReadVector(fp, *ncol+1, *colptr, colnum, colsize);
                    ReadVector(fp, *nonz, *rowind, rownum, rowsize);
                    if ( numer_lines ) {
                        sReadValues(fp, *nonz, *nzval, valnum, valsize);
                    }

                    fclose(fp);
                }

            };
        };
    };
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Sea Surveillance AS
Norway Norway
Chief Architect - Sea Surveillance AS.

Specializing in integrated operations and high performance computing solutions.

I’ve been fooling around with computers since the early eighties, I’ve even done work on CP/M and MP/M.

Wrote my first “real” program on a BBC micro model B based on a series in a magazine at that time. It was fun and I got hooked on this thing called programming ...

A few Highlights:

  • High performance application server development
  • Model Driven Architecture and Code generators
  • Real-Time Distributed Solutions
  • C, C++, C#, Java, TSQL, PL/SQL, Delphi, ActionScript, Perl, Rexx
  • Microsoft SQL Server, Oracle RDBMS, IBM DB2, PostGreSQL
  • AMQP, Apache qpid, RabbitMQ, Microsoft Message Queuing, IBM WebSphereMQ, Oracle TuxidoMQ
  • Oracle WebLogic, IBM WebSphere
  • Corba, COM, DCE, WCF
  • AspenTech InfoPlus.21(IP21), OsiSoft PI


More information about what I do for a living can be found at: harlinn.com or LinkedIn

You can contact me at espen@harlinn.no

Comments and Discussions