#include #include #include "array.h" #include using namespace std; template ArrayFP::ArrayFP(unsigned int dim) { N = dim; if (N == 0) entry = 0; else entry = new T[N]; reset(); } template ArrayFP::~ArrayFP(){ if (entry != 0) delete [] entry; } template void ArrayFP::setup(int dim) { N = dim; if (N == 0) entry = 0; else entry = new T[N]; reset(); } template T ArrayFP::magnitude() { int i; T mag; mag = 0.0; for (i = 0; i < N; i ++) { mag += (entry[i] * entry[i]); } mag = sqrt(mag); return mag; } template T ArrayFP::getentry(int m) const { return entry[m]; } template void ArrayFP::addArray(const ArrayFP &xvtr) { int i; T cval; if (xvtr.N != N) return; for (i = 0; i < N; i ++) { cval = xvtr.getentry(i); entry[i] = entry[i] + cval; } } template T ArrayFP::operator * (const ArrayFP &operand2) const { T cval = 0.0; int i; for (i = 0; i < N; i ++) { cval = cval + entry[i] * operand2.entry[i]; } return cval; } template ArrayFP& ArrayFP::operator=(const ArrayFP &right) { // int i; // N = right.N; // entry = new T[N]; // for (i = 0; i < N; i ++) entry[i] = right.entry[i]; return *this; } template <> ArrayFP& ArrayFP::operator=(const ArrayFP &right) { cblas_scopy(right.N, right.entry, 1, entry, 1); return *this; } template <> ArrayFP& ArrayFP::operator=(const ArrayFP &right) { cblas_dcopy(right.N, right.entry, 1, entry, 1); return *this; } template void ArrayFP::print() { cout.setf(ios::scientific); cout.precision(15); cout << "begin"<< endl; for (int i = 0 ; i < N ; i++) cout << "x(" << i+1 << ") = " << entry[i] << endl; cout << "end"<< endl; } template void ArrayFP::print(char* name) { cout << name << endl; cout.setf(ios::scientific); cout.precision(7); cout << "begin"<< endl; int M=N/12; for (int j = 0 ; j < M ; j++){ for (int i = 0 ; i < 12 ; i++){ // cout << "x(" << i+1 << ") = " << entry[j*12+i] <<" "; cout << entry[j*12+i] << " "; }cout << endl; } cout << "end"<< endl; } // y = alpha*x template void ArrayFP::ax(T alpha, ArrayFP& x) { } template <> void ArrayFP::ax(float alpha, ArrayFP& x) { cblas_scopy(N, x.entry, 1, entry, 1); cblas_sscal(N, alpha, entry, 1); } template <> void ArrayFP::ax(double alpha, ArrayFP& x) { cblas_dcopy(N, x.entry, 1, entry, 1); cblas_dscal(N, alpha, entry, 1); } // y = alpha*x + y template void ArrayFP::axpy(T alpha, ArrayFP& x) { } template <> void ArrayFP::axpy(float alpha, ArrayFP& x) { cblas_saxpy(N, alpha, x.entry, 1, entry, 1); } template <> void ArrayFP::axpy(double alpha, ArrayFP& x) { cblas_daxpy(N, alpha, x.entry, 1, entry, 1); } // y = alpha*x + beta*y template void ArrayFP::axpby(T alpha, T beta, ArrayFP& x) { } template <> void ArrayFP::axpby(float alpha, float beta, ArrayFP& x) { cblas_sscal(N, beta, entry, 1); cblas_saxpy(N, alpha, x.entry, 1, entry, 1); } template <> void ArrayFP::axpby(double alpha, double beta, ArrayFP& x) { cblas_dscal(N, beta, entry, 1); cblas_daxpy(N, alpha, x.entry, 1, entry, 1); } // y = x + beta*y template void ArrayFP::xpby(T beta, ArrayFP& x) { // #pragma omp parallel for schedule(static) // for (int i = 0; i < dim_; i++) p_[i] = x.p_[i] + beta*p_[i]; axpby(1., beta, x); } template void ArrayFP::WriteToASCII(char* FileName, int& timeStep) { char Field_TimeLog[180]; // sprintf(Field_TimeLog, "%s_%d.ico", FileName, timeStep); sprintf(Field_TimeLog, "%s", FileName); ofstream Outfile(Field_TimeLog, ios_base::out); Outfile.setf(ios::scientific, ios::floatfield); Outfile.precision(15); if(!Outfile) cout << "ArrayFP:: Error in opening file: " << Field_TimeLog << " for write " << endl; Outfile << timeStep << '\n'; for (int i = 0 ; i < N ; i++) Outfile << entry[i] << '\n'; Outfile.close(); } template void ArrayFP::ReadFromASCII(char* FileName, int& timeStep) { char Field_TimeLog[180]; T CurrentValue = 0.0; // sprintf(Field_TimeLog, "%s_%d.ico", FileName, timeStep); sprintf(Field_TimeLog, "%s", FileName); ifstream InputFile(Field_TimeLog, ios_base::in); if (!InputFile) cout << "ArrayFP:: Can not open file: " << Field_TimeLog << "for read" << endl; InputFile >> timeStep; for (int i = 0 ; i < N ; i++){ InputFile >> CurrentValue; entry[i] = CurrentValue; } InputFile.close(); } template void ArrayFP::WriteToASCII(char* FileName) { char Field_TimeLog[180]; sprintf(Field_TimeLog, "%s", FileName); ofstream Outfile(Field_TimeLog, ios_base::out); Outfile.setf(ios::scientific, ios::floatfield); Outfile.precision(15); if(!Outfile) cout << "ArrayFP:: Error in opening file: " << Field_TimeLog << " for write " << endl; for (int i = 0 ; i < N ; i++) Outfile << entry[i] << '\n'; Outfile.close(); } template void ArrayFP::ReadFromASCII(char* FileName) { char Field_TimeLog[180]; T CurrentValue = 0.0; sprintf(Field_TimeLog, "%s", FileName); ifstream InputFile(Field_TimeLog, ios_base::in); if (!InputFile) cout << "ArrayFP:: Can not open file: " << Field_TimeLog << "for read" << endl; for (int i = 0 ; i < N ; i++){ InputFile >> CurrentValue; setentry(i, CurrentValue); } InputFile.close(); } template void ArrayFP::WriteToBinary(char* FileName) { // char Field_TimeLog[180]; // sprintf(Field_TimeLog, "%s", FileName); // ofstream Outfile(Field_TimeLog, ios_base::out, ios::binary); // if(!Outfile) // cout << "ArrayFP:: Error in opening file: " << Field_TimeLog << " for write " << endl; // // buffer = new char[size]; // T value; // for (int i = 0 ; i < N ; i++){ // value = entry[i]; // Outfile.write(buffer, value); // } // Outfile.close(); } template void ArrayFP::ReadFromBinary(char* FileName) { // char Field_TimeLog[180]; // T CurrentValue = 0.0; // sprintf(Field_TimeLog, "%s", FileName); // ifstream InputFile(Field_TimeLog, ios_base::in, ios::binary); // if (!InputFile) // cout << "ArrayFP:: Can not open file: " << Field_TimeLog << "for read" << endl; // // for (int i = 0 ; i < N ; i++){ // InputFile.read((char *)(&CurrentValue), sizeof(CurrentValue)); // setentry(i, CurrentValue); // } // InputFile.close(); }