Demo entry 3520991

Block CSR

   

Submitted by BCSR on Jan 08, 2016 at 16:39
Language: C++. Code size: 4.4 kB.

//------------- Clase Compressed Sparse Row CSR -------------
// ---------------- Maestría en Matemática ------------------
// --------------- Álgebra Lineal Numérica ------------------
// ---------------------Realizado por: ----------------------
// ------------- Angel Roberto Rivera Muñoz -----------------

#include<iostream>
#include<fstream>
#include<cassert>
#include<iomanip>
#include "bcsr.h"
#include "dense_Matriz.h"

using namespace std;
typedef unsigned int uint;

bcsr::bcsr() {
	bnrows_ = 0;
	bncols_ = 0;
}

bcsr::~bcsr() {
    cout << "Entro aquí\n";
    if(jrows_ != NULL && jcols_ != NULL && aa_ != NULL){
	for (uint i = 0; i < bnrows_; i++) {
                delete[] jrows_[i];
                delete[] jcols_[i];
                delete[] aa_[i];
        }

	delete[] nrows_;
	delete[] ncols_;
	delete[] bnnzi_;
	delete[] jrows_;
	delete[] jcols_;
	delete[] aa_;
	delete[] bia_;
    }
}

void bcsr::read(const char filename[])
{
	double **matriz;
	int nrow, ncol;
	ifstream fin;
	fin.open(filename, ios::in);

	if (fin.fail()) {
		cerr << "ERROR DE LECTURA DE ARCHIVO" << filename << endl;
		exit(EXIT_FAILURE);
	}

	fin >> bnrows_;
	fin >> bncols_;

	nrows_ = new uint[bnrows_]; assert(nrows_ != 0);
	ncols_ = new uint[bncols_]; assert(ncols_ != 0);
	bnnzi_ = new uint[bnrows_]; assert(bnnzi_ != 0);
	bia_ = new uint[bncols_ + 1]; assert(bia_ != 0);
	jrows_ = new uint*[bnrows_]; assert(jrows_ != 0);
	jcols_ = new uint*[bnrows_]; assert(jcols_ != 0);
	aa_ = new dense_Matriz*[bnrows_]; assert(aa_ != 0);

	for (uint i = 0; i < bnrows_; i++) {
		fin >> nrows_[i];
	}

	for (uint i = 0; i < bncols_; i++) {
		fin >> ncols_[i];
	}

	for (uint i = 0; i < bnrows_; i++){
		fin >> bnnzi_[i];

		jrows_[i] = new uint[bnnzi_[i]];
		jcols_[i] = new uint[bnnzi_[i]];
		aa_[i] = new dense_Matriz[bnnzi_[i]];

		for (uint j = 0; j < bnnzi_[i]; j++) {
			fin >> jrows_[i][j];
			fin >> jcols_[i][j];

			fin >> nrow;
			fin >> ncol;

            matriz = new double*[nrow];
            for (int l = 0; l < nrow; l++) matriz[l] = new double[ncol];

			for (int r = 0; r < nrow; r++) {
				for (int c = 0; c < ncol; c++) {
					fin >> matriz[r][c];
				}
			}
			aa_[i][j].setMatriz(matriz,nrow);

			for (int r = 0; r < nrow; r++) {
				delete[] matriz[r];
			}
			delete[] matriz;
		}
	}

	bia_[0] = 0;
	for (uint i = 0; i <  bnrows_ + 1; i++) {
		bia_[i+1] = bnnzi_[i] + bia_[i];
	}
	fin.close();

}

void bcsr::write(const char filename[]) const {
	ofstream fout(filename);

	fout << setprecision(20) << scientific;
	fout << bnrows_ << '\n';
	fout << bncols_ << '\n';

	for (uint i = 0; i < bnrows_; i++) fout << nrows_[i] << '\n';
	for (uint i = 0; i < bncols_; i++) fout << ncols_[i] << '\n';

	for (uint i = 0; i < bnrows_; i++) {
		fout << bnnzi_[i] << '\n';
		for (uint j = 0; j < bnnzi_[i]; j++) {
			fout << jrows_[i][j] << " " << jcols_[i][j] << '\n';
			fout << aa_[i][j].getRows() << " " << aa_[i][j].getCols() << '\n';
			for (uint r = 0; r< aa_[i][j].getRows(); r++) {
				for (uint c = 0; c < aa_[i][j].getCols(); c++) {
					fout << aa_[i][j].getElement(r, c) << '\n';
				}
			}
		}
	}
	fout.close();
}

void bcsr::print()
{
	cout << "bnrows: " << bnrows_ << endl;
	cout << "bncols: " << bncols_ << endl;

	cout << "\nnrows: ";
	for (uint i = 0; i < bnrows_; i++)
		cout << nrows_[i] << "  ";

	cout << "\nncols: ";
	for (uint i = 0; i < bnrows_; i++)
		cout << ncols_[i] << "  ";

	cout << endl << "bnnzi: ";
	for (uint i = 0; i < bnrows_; i++)
		cout << bnnzi_[i] << "  ";

	cout << "\nbia  :";
	for (uint i = 0; i < bncols_ + 1; i++) cout << bia_[i] << "  ";

	cout << "\njrows \t   jcols\n";
	for (uint i = 0; i < bnrows_; i++)
		for (uint j = 0; j < bnnzi_[i]; j++) {
			cout << jrows_[i][j] << "\t\t" << jcols_[i][j] << "\n";
		}

	cout << endl << "Bloque de matrices AA:\n ";
	for (uint i = 0; i < bnrows_; i++){
		cout << "\n Bloque fila " << i+1 << "\n";
		for (uint j = 0; j < bnnzi_[i]; j++) {
			cout << aa_[i][j] << "\n ";
		}
	}
	cout << endl;
}


uint bcsr::getBnrows() const
{
	return bnrows_;
}

uint bcsr::getBncols() const
{
	return bncols_;
}

uint bcsr::getBnnzi() const
{
	return bia_[bnrows_];
}

uint bcsr::getBnnzi(uint i) const
{
	return bnnzi_[i];
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).