Re: [eigen] Cost of a map operation

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


> strange. can you show the compiler errors?

Sorry for the 5 files involved. The offending function is init(), in
analysis2.cpp. You will need to do make nocds to compile. Also you
will have to tweak the eigen include path in the Makefile. Loads of
stuff has been commented out in analysis2.cpp. You can ignore the
binary* files, they are *certainly* not a problem, but they need to be
there.

Just put all the files in one directory, it should be good enough. I
am using gcc4.4.1, 64 bit, eigen unstable (but not the latest version
of eigen). I think the issue here is some kind of syntax error. Here
it is,

==========
analysis2.cpp:154: error: declaration of
‘Eigen::Map<Eigen::Matrix<int, 33331, 33331, 0, 33331, 33331>, 0>
dataMatrix’ shadows a parameter
==========

-- 
Rohit Garg

http://rpg-314.blogspot.com/

Senior Undergraduate
Department of Physics
Indian Institute of Technology
Bombay
/*

This file is for NOT performing CDS and then plotting stuff (mu and sigma).

 */

#include<Eigen/Core>
#include<Eigen/Array>
#include<binary_matrix_io.h>
#include<constants.h>
#include<iostream>
#include<string>
#include<cstdio>
#include<gsl/gsl_histogram.h>
#include <gsl/gsl_statistics.h>
using namespace std;
using namespace Eigen;

int init(const char * dataFile, MatrixXi &dataMatrix, binmatio_packet_t *);

int main()
    {
//verify the size of our header
    cout<<"size of header "<<sizeof(binmatio_header_t)<<endl;

    MatrixXi dataMatrix(1,1);

    binmatio_packet_t packet;
    init("adc_channel7",dataMatrix, &packet);
//     cout<<"file read\n";
// //we define 3 sets of sigma and mu
// //for each of the three we need an accumulator and a squared accumulator(fp ofcourse)

//     const int framesWithoutCDS=framesPerShot;

// //    VectorXf x_accum[framesWithoutCDS],x2_accum[framesWithoutCDS];
//     gsl_histogram * histograms[framesWithoutCDS];//one for each of mu and sigma
//     FILE* outFiles[framesWithoutCDS];

//     int i,j;//generic indices
// ////step 0, init the accumulators to 0
// //    for(i=0; i<framesWithoutCDS; i++)
// //	{
// //	x_accum[i].setZero(chipPixels);
// //	x2_accum[i].setZero(chipPixels);
// //	}
// //step 1, perform CDS, scale and translate and then cast to float

// 	typedef Matrix<float, chipPixels, 1> chipAsColumnMatrixType;	
	
//     chipAsColumnMatrixType x_accum[framesWithoutCDS],x2_accum[framesWithoutCDS];
//     for(i=0; i<framesWithoutCDS; i++)
//     	{
// 			x_accum[i].setZero();
// 			x2_accum[i].setZero();
//     	}
    
//   chipAsColumnMatrixType  inFloat;//the temporary to hold the value of CDS/cast

//     int N=dataMatrix.cols();//the N from the statistics definitions

// 	MatrixXd gsl_buffer[framesWithoutCDS];//the buffer used to cast-to-double and store, so that we can use gsl to calculate 
// //	the sigma's
// //for(i=0; i<framesWithoutCDS; i++)
// //	{
// //	gsl_buffer[i]=MatrixXd::Random(chipPixels, N);
// //	}
// ////resized the buffers
// for(i=0; i<framesWithoutCDS; i++)
// 	{
// 	gsl_buffer[i]=dataMatrix.block(i*chipPixels,0,chipPixels,N).cast<double>();
// 	cout<<"printing "<<i<<'\n'<<gsl_buffer[i].corner(TopLeft,5,5);
// 	}	
// //copied everything out to gsl_buffer	



// //    for(j=0; j<N; j++)//for each column
// //	{
// //	for(i=0; i<framesWithoutCDS; i++)
// //	    {
// //	    inFloat=(dataMatrix.col(j).segment<chipPixels>(i*chipPixels)).cast<float>();
// //	    x_accum[i]+=inFloat;//step 2 is here as well
// //	    x2_accum[i]+=(inFloat.cwise()*inFloat);
// //	    }
// //	}

// //    chipAsColumnMatrixType mu[framesWithoutCDS],sigma[framesWithoutCDS];
// ////step 4, final calculations
// //    for(i=0; i<framesWithoutCDS; i++)
// //	{
// //	mu[i]=x_accum[i]*(1.0/N);
// //	sigma[i]=(x2_accum[i]*(1.0/N)-mu[i].cwise()*mu[i]).cwise().sqrt();
// //	}
// ////v.cwise().sqrt()

//     string muFiles("muFile");
//     string sigmaFiles("sigmaFile");
//     string extension(".txt");
    
// ////do it for the mu's
// //    for(i=0; i<framesWithoutCDS; i++)
// //	{
// //	histograms[i]= gsl_histogram_alloc(100);
// //        gsl_histogram_set_ranges_uniform (histograms[i], 2000,2060);
// //	outFiles[i]=fopen((muFiles+char(i+'0')+extension).c_str(),"w");
// //	for(j=0; j<chipPixels; j++)
// //	    {
// //	    gsl_histogram_accumulate (histograms[i], mu[i][j],1.0);
// //	    }
// //	gsl_histogram_fprintf (outFiles[i], histograms[i], "%f", "%f");
// //	fclose(outFiles[i]);
// //	gsl_histogram_free (histograms[i]);
// //	}
// //do it for the sigma's
//     for(i=0; i<framesWithoutCDS; i++)
// 	{
// //	histograms[i]= gsl_histogram_alloc(50);
// //        gsl_histogram_set_ranges_uniform (histograms[i],-2, 5);
// 	outFiles[i]=fopen((sigmaFiles+(char('0'+i))+extension).c_str(),"w");
// 	for(j=0; j<chipPixels; j++)
// 	    {
// 	    fprintf(outFiles[i], "%f\n",
// 	    	gsl_stats_sd(gsl_buffer[i].data(), chipPixels, N)
// 	    	);
// 	    }
// //	gsl_histogram_fprintf (outFiles[i], histograms[i], "%f", "%f");
// 	fclose(outFiles[i]);
// //	gsl_histogram_free (histograms[i]);
// 	}
    binmatio_commit(&packet);
    return 0;
    } 


int init(const char * dataFile, MatrixXi &dataMatrix, binmatio_packet_t *packetPtr)
    {
    packetPtr->file_des=open (dataFile, O_RDONLY);

    char status=binmatio_read(PROT_READ, packetPtr);

//     cout<<"\nread status = "<<status;

// // //the readout reveals
//     cout<<"\nheader_version = "<<packetPtr->fileHeader.header_version;
//     cout<<"\nrows = "<<packetPtr->fileHeader.rows;
//     cout<<"\ncolumns = "<<packetPtr->fileHeader.columns;
//     cout<<"\ndatatype = "<<packetPtr->fileHeader.datatype;
//     cout<<"\nstorage_order = "<<packetPtr->fileHeader.storage_order;

    int32_t *dataPtr=(int32_t*)(packetPtr->dataPtr+BINMATIO_HEADER_WIDTH);

//    static AlignedMapType 	MapAligned (Scalar *data, int rows, int cols);
		Map<MatrixXi, ForceAligned> dataMatrix(dataPtr,packetPtr->fileHeader.rows,packetPtr->fileHeader.columns);	
//    dataMatrix = Map<MatrixXi,ForceAligned>(dataPtr,packetPtr->fileHeader.rows,packetPtr->fileHeader.columns);
//    cout<<dataMatrix.corner(TopLeft,5,5);
    return status;
    }
#ifndef __BINARY_MATRIX_IO__
#define __BINARY_MATRIX_IO__

#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
typedef enum
    {
    uint8_type,
    int8_type,
    uint16_type,
    int16_type,
    uint32_type,
    int32_type,
    uint64_type,
    int64_type,
    float_type,
    double_type
    }datatype_t;

/*
header_version = 0 is reserved
header_version = 1 is being used, it's the simplest one
*/

#define BINMATIO_HEADER_WIDTH (64)

typedef enum
    {
    column_major,
    row_major
    }storage_order_t;

typedef struct 
    {
    uint64_t rows,columns;
    datatype_t datatype;
    storage_order_t storage_order;
    char header_version;
    char padding[BINMATIO_HEADER_WIDTH - sizeof(char)-2*sizeof(uint64_t)-sizeof(datatype_t)-sizeof(storage_order_t)];
    }binmatio_header_t;//meant to be written to disk

typedef struct
    {
    binmatio_header_t fileHeader;
    char *dataPtr;//the pointer to the real disk data, includes the header,
//so for real data location, write at dataPtr+64
    unsigned char size_per_element;
    int file_des;
    }binmatio_packet_t;//meant to be kept in memory

//follows OS convention in error return calls
//0 is ok, all else means error
char binmatio_read(const int prot, binmatio_packet_t * packetPtr);
/*
this function will load up the data into packetPtr
 */

char binmatio_create(const int prot, binmatio_packet_t *packetPtr);

//prt in the abovetwo function denotes the flags used for mmap only. 

int binmatio_commit(const binmatio_packet_t * packetPtr);

#endif
#include<binary_matrix_io.h>
#include <fcntl.h>
#include <unistd.h>

char size(datatype_t datatype)
    {
    switch(datatype)
	{
	case uint64_type:return 8;
	case uint32_type:return 4;
	case uint16_type:return 2;
	case uint8_type:return 1;
	case int8_type:return 1;
	case int16_type:return 2;
	case int32_type:return 4;
	case int64_type:return 8;
	case float_type:return 4;
	case double_type:return 8;
	default :return 0;//an error condition
	}
    }

char binmatio_read(int prot, binmatio_packet_t * packetPtr)
    {
    struct stat statbuf;
        
    void *src;

    if(packetPtr == NULL)
	{
	fprintf(stderr,"binmatio_read(): NULL pointer passed in\n");
	return 3;
	}

//fstat the file to find it's size first
    /* find size of input file */
    if (fstat (packetPtr->file_des,&statbuf) < 0)
	{
	fprintf(stderr,"binmatio_read(): fstat error\n");
	return 1;
	}

    /* now mmap the input file */
    if ((src = mmap (NULL, statbuf.st_size, prot, MAP_SHARED, packetPtr->file_des, 0)) == (caddr_t) -1)
	{
	fprintf(stderr,"binmatio_read(): mmap error for input\n");
	return 2;
	}

    packetPtr->fileHeader       = ((binmatio_packet_t*)src)->fileHeader;//read it from disk to memory
    packetPtr->dataPtr          = (char*)src;
    packetPtr->size_per_element = size(packetPtr->fileHeader.datatype);
    return 0;
    }

char binmatio_create(int prot, binmatio_packet_t *packetPtr)
    {
    if(packetPtr == NULL)
	{
	fprintf(stderr,"binmatio_create(): NULL pointer passed in for packetPtr\n");
	return 4;
	}

    packetPtr->size_per_element=size(packetPtr->fileHeader.datatype);

    uint64_t bufSize= (packetPtr->size_per_element)*
	(packetPtr->fileHeader.rows)*
	(packetPtr->fileHeader.columns)+
	sizeof(binmatio_header_t);

    /* go to the location corresponding to the last byte */
    if (lseek (packetPtr->file_des, bufSize - 1, SEEK_SET) == -1)
	{
	fprintf(stderr,"binmatio_create():lseek error\n");
	return 1;
	}
 
    /* write a dummy byte at the last location */
    if (write (packetPtr->file_des, "", 1) != 1)
	{
	fprintf(stderr,"binmatio_create():write error\n");
	return 2;
	}

    void *src;

    /* mmap the output file */
    if ((src = (char*)mmap (NULL, bufSize, prot, MAP_SHARED, packetPtr->file_des, 0)) == (caddr_t) -1)
	{
	fprintf(stderr,"binmatio_create():mmap error for output\n");
	return 3;
	}

    packetPtr->dataPtr          = (char*)src;

    *((binmatio_header_t*)packetPtr->dataPtr)=packetPtr->fileHeader;//wrote the binmatio_header_t to disk
    return 0;
    }

int binmatio_commit(const binmatio_packet_t * packetPtr)
    {
    return munmap(packetPtr->dataPtr, (packetPtr->size_per_element)*(packetPtr->fileHeader.rows)*(packetPtr->fileHeader.columns)) || close(packetPtr->file_des);
    }

Attachment: Makefile
Description: Binary data

#ifndef __CONSTANTS__
#define __CONSTANTS__

#define chipRows        (96)
#define chipColumns     (48)
#define chipPixels      (chipRows*chipColumns)
//=416
#define framesPerShot   (2)
#define pixelCount      (chipPixels*framesPerShot)
//=1664
//for 4 frames in one shot, ie per marker
#define shotsPerRead    (1)
//as defined by sunxm, 5 markers in one read

#endif


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/