4

Here comes a new issue after my previous question :

I've extended the code to perform matrix binary files I/O and when testing a simple write and read operation, I retrieved only the first line of the matrix...

I don't have managed to find my error, here is the new code :

double** bytes_to_matrix_block(std::ifstream& iF, int size1, int size2) {
    double** m = new double*[size1];
    double read;
    int i = 0, j = 0;

    if(!iF) {
        std::cout << "opening file for reading error";
        throw 1;
    }
    while(i < size1 && !iF.eof()) {
        m[i] = new double[size2];
        while(j < size2 && !iF.eof()) {
            iF.read( reinterpret_cast<char*>( &read ), sizeof read );
            m[i][j] = read;
            std::cout << read << ", ";
            j++;
        }
        std::cout << std::endl;
        i++;
    }
    if(i < size1 || j < size2) {
        std::cout << "premature end of file while reading..." << std::endl;
        throw 1;
    }
    return m;
}

void matrix_block_to_bytes(double** m, int size1, int size2, std::ofstream& oF){

    if(!oF){
        std::cout << "opening file for writing error";
        throw 1;
    }

    double cdbl;

    for(int i = 0; i < size1; i++){
        for(int j = 0; j < size2; j++){
            cdbl = m[i][j];
            std::cout << cdbl << ", ";
            oF.write( reinterpret_cast<char*>( &cdbl ), sizeof cdbl );
        }
        std::cout << std::endl;
    }
}

Thanks by advance

Community
  • 1
  • 1
Andy
  • 429
  • 1
  • 6
  • 17
  • 1
    Irrelevant to the problem, but just a hint -- you may wanna store the matrices as `double*` instead of `double**` in column or row major, or you won't be able to use any of the common BLAS libraries if you ever need one, at least not easily... Also, you may wanna read/write arrays at once, not `double`s, as it will be way more efficient... – TC1 Jul 26 '11 at 14:14
  • I've already used boost/ublas library and it works fine but as I only perform matrix multiplication, I do it with double** and it's more efficient (between three and four times) and it permit better use of multi-threading. About reading/writing arrays at once, I'll think of it ;) thanks – Andy Jul 26 '11 at 14:29
  • 1
    uBlas is actually one of the slowest around, you may wanna look into Eigen, it multi-threads matrix*matrix via OpenMP. – TC1 Jul 26 '11 at 14:38
  • it's a pity that it doesn't multi-threads matrix*vector, that's what i do... via openMP too. Do you know if it multi-threads LU decomposition and inversion ? It may be interesting for another work – Andy Jul 26 '11 at 14:49
  • It doesn't multi-thread m*v for a reason, and neither does BLAS/LAPACK and any implementation of them (MKL, ACML, ATLAS and whatever else), it works way faster without multi-threading because with it it spends more time syncing than actually doing stuff, which is not the case for m*m. In my own tests, an OpenMP for loop was usually ~10x slower than any BLAS implementation that used a single thread. About the LU -- no idea tbh, but you could probably easily try it, the interface is simple & they added some pretty nifty performance tuning to Eigen in version 3. – TC1 Jul 26 '11 at 17:47

1 Answers1

1

When reading, you forget to reset j to 0 for new lines

while(i < size1 && !iF.eof()) {
// Missing:
j = 0;
b.buchhold
  • 3,837
  • 2
  • 24
  • 33