Ejemplo n.º 1
0
int main() 
{
  // Change this type definition to double if your gpu supports that
  typedef float       ScalarType;
  
  // Create vectors of eight complex values (represented as pairs of floating point values: [real_0, imag_0, real_1, imag_1, etc.])
  viennacl::vector<ScalarType> input_vec(16);  
  viennacl::vector<ScalarType> output_vec(16); 
  
  // Fill with values (use viennacl::copy() for larger data!)
  for (std::size_t i=0; i<input_vec.size(); ++i)
  {
    if (i%2 == 0)
      input_vec(i) = ScalarType(i/2);  // even indices represent real part
    else
      input_vec(i) = 0;                // odd indices represent imaginary part
  }
  
  // Print the vector
  std::cout << "input_vec: " << input_vec << std::endl;
  
  // Compute FFT and store result in 'output_vec'
  std::cout << "Computing FFT..." << std::endl;
  viennacl::fft(input_vec, output_vec);
  
  // Compute FFT and store result directly in 'input_vec'
  viennacl::inplace_fft(input_vec);
  
  // Print result
  std::cout << "input_vec: " << input_vec << std::endl;
  std::cout << "output_vec: " << output_vec << std::endl;

  std::cout << "Computing inverse FFT..." << std::endl;
  viennacl::ifft(input_vec, output_vec); // either store result into output_vec
  viennacl::inplace_ifft(input_vec);     // or compute in-place  
  
  std::cout << "input_vec: " << input_vec << std::endl;
  std::cout << "output_vec: " << output_vec << std::endl;
  
  //
  //  That's it.
  //
  std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;

  return EXIT_SUCCESS;
}
Ejemplo n.º 2
0
int main()
{
  FILE *fin,*fout;
  double **a,*b;
  int i;
  //open file//
  a =dmatrix(1,N,1,N);
  b =dvec(1,N);

  fin = fopen("input.dat","r");
  if (fin ==NULL)
    {
      printf("Can't find file\n");
      exit(1);
    }
  fout = fopen("output.dat","w");
  if(fout == NULL)
    {
      printf("Can't make file\n");
      exit(1);
    }
  input_matrix(a,'A',fin,fout);
  input_vec(b,'b',fin,fout);
  //  printf("%lf",a[1][1]);
  b =simple_gauss(a,b);

  //output results//
  fprintf(fout,"Ax=bの計算結果は次の通り\n");
  for(i = 1;i <= N; i++)
    {
      fprintf(fout,"%f\n",b[i]);
    }
  
  fclose(fin);fclose(fout);

  //  free_dmatrix(a,1,N,1,N);free_dvec(b,1);
  return(0);
}
Ejemplo n.º 3
0
/* Resample a signal vector
 *
 * The input vector is deallocated and the pointer returned with a vector
 * of any unconverted samples.
 */
signalVector *resmpl_sigvec(signalVector *hist, signalVector **vec,
			    signalVector *lpf, double in_rate,
			    double out_rate, int chunk_sz)
{
	signalVector *resamp_vec;
	int num_chunks = (*vec)->size() / chunk_sz;

	/* Truncate to a chunk multiple */
	signalVector trunc_vec(num_chunks * chunk_sz);
	(*vec)->segmentCopyTo(trunc_vec, 0, num_chunks * chunk_sz);

	/* Update sample buffer with remainder */
	*vec = segment(*vec, trunc_vec.size(), (*vec)->size() - trunc_vec.size());

	/* Add history and resample */
	signalVector input_vec(*hist, trunc_vec);
	resamp_vec = polyphaseResampleVector(input_vec, in_rate,
					     out_rate, lpf);

	/* Update history */
	trunc_vec.segmentCopyTo(*hist, trunc_vec.size() - hist->size(),
				hist->size());
	return resamp_vec;
}
Ejemplo n.º 4
0
/*
void jdsmv(int height, int len, float* value, int* perm, int* jds_ptr, int* col_index, float* vector,
        float* result){
        int i;
        int col,row;
        int row_index =0;
        int prem_indicator=0;
        for (i=0; i<len; i++){
                if (i>=jds_ptr[prem_indicator+1]){
                        prem_indicator++;
                        row_index=0;
                }
                if (row_index<height){
                col = col_index[i];
                row = perm[row_index];
                result[row]+=value[i]*vector[col];
                }

                row_index++;
        }
        return;
}
*/
int main(int argc, char** argv) {
	struct pb_TimerSet timers;
	struct pb_Parameters *parameters;
	
	
	
	
	
	printf("CPU-based sparse matrix vector multiplication****\n");
	printf("Original version by Li-Wen Chang <*****@*****.**> and Shengzhao Wu<*****@*****.**>\n");
	printf("This version maintained by Chris Rodrigues  ***********\n");
	parameters = pb_ReadParameters(&argc, argv);
	if ((parameters->inpFiles[0] == NULL) || (parameters->inpFiles[1] == NULL))
    {
      fprintf(stderr, "Expecting two input filenames\n");
      exit(-1);
    }

	
	pb_InitializeTimerSet(&timers);
	pb_SwitchToTimer(&timers, pb_TimerID_COMPUTE);
	
	//parameters declaration
	int len;
	int depth;
	int dim;
	int pad=1;
	int nzcnt_len;
	
	//host memory allocation
	//matrix
	float *h_data;
	int *h_indices;
	int *h_ptr;
	int *h_perm;
	int *h_nzcnt;
	//vector
	float *h_Ax_vector;
    float *h_x_vector;
	
	
    //load matrix from files
	pb_SwitchToTimer(&timers, pb_TimerID_IO);
	//inputData(parameters->inpFiles[0], &len, &depth, &dim,&nzcnt_len,&pad,
	//    &h_data, &h_indices, &h_ptr,
	//    &h_perm, &h_nzcnt);

 

	int col_count;
	coo_to_jds(
		parameters->inpFiles[0], // bcsstk32.mtx, fidapm05.mtx, jgl009.mtx
		1, // row padding
		pad, // warp size
		1, // pack size
		1, // is mirrored?
		0, // binary matrix
		1, // debug level [0:2]
		&h_data, &h_ptr, &h_nzcnt, &h_indices, &h_perm,
		&col_count, &dim, &len, &nzcnt_len, &depth
	);		

  h_Ax_vector=(float*)malloc(sizeof(float)*dim);
  h_x_vector=(float*)malloc(sizeof(float)*dim);
//  generate_vector(h_x_vector, dim);
  input_vec( parameters->inpFiles[1],h_x_vector,dim);

	
	pb_SwitchToTimer(&timers, pb_TimerID_COMPUTE);

	
  int p, i;
	//main execution
	for(p=0;p<50;p++)
	{
    #pragma omp parallel for
		for (i = 0; i < dim; i++) {
      int k;
		  float sum = 0.0f;
		  //int  bound = h_nzcnt[i / 32];
		  int  bound = h_nzcnt[i];
		  for(k=0;k<bound;k++ ) {
			int j = h_ptr[k] + i;
			int in = h_indices[j];

			float d = h_data[j];
			float t = h_x_vector[in];

			sum += d*t;
		  }
    //  #pragma omp critical 
		  h_Ax_vector[h_perm[i]] = sum;
		}
	}	

	if (parameters->outFile) {
		pb_SwitchToTimer(&timers, pb_TimerID_IO);
		outputData(parameters->outFile,h_Ax_vector,dim);
		
	}
	pb_SwitchToTimer(&timers, pb_TimerID_COMPUTE);
	
	free (h_data);
	free (h_indices);
	free (h_ptr);
	free (h_perm);
	free (h_nzcnt);
	free (h_Ax_vector);
	free (h_x_vector);
	pb_SwitchToTimer(&timers, pb_TimerID_NONE);

	pb_PrintTimerSet(&timers);
	pb_FreeParameters(parameters);

	return 0;

}
Ejemplo n.º 5
0
/**
*   In the main()-routine we create a few vectors and matrices and then run FFT on them.
**/
int main()
{
  // Feel free to change this type definition to double if your gpu supports that
  typedef float ScalarType;

  // Create vectors of eight complex values (represented as pairs of floating point values: [real_0, imag_0, real_1, imag_1, etc.])
  viennacl::vector<ScalarType> input_vec(16);
  viennacl::vector<ScalarType> output_vec(16);
  viennacl::vector<ScalarType> input2_vec(16);

  viennacl::matrix<ScalarType> m(4, 8);
  viennacl::matrix<ScalarType> o(4, 8);

  for (std::size_t i = 0; i < m.size1(); i++)
    for (std::size_t s = 0; s < m.size2(); s++)
      m(i, s) = ScalarType((i + s) / 2);

  /**
  *  Fill the vectors and matrices with values by using operator(). Use viennacl::copy() for larger data!
  **/
  for (std::size_t i = 0; i < input_vec.size(); ++i)
  {
    if (i % 2 == 0)
    {
      input_vec(i) = ScalarType(i / 2); // even indices represent real part
      input2_vec(i) = ScalarType(i / 2);
    } else
      input_vec(i) = 0;            // odd indices represent imaginary part
  }

  /**
  * Compute the FFT and store result in 'output_vec'
  **/
  std::cout << "Computing FFT Matrix" << std::endl;
  std::cout << "m: " << m << std::endl;
  std::cout << "o: " << o << std::endl;
  viennacl::fft(m, o);
  std::cout << "Done" << std::endl;
  std::cout << "m: " << m << std::endl;
  std::cout << "o: " << o << std::endl;
  std::cout << "Transpose" << std::endl;

  viennacl::linalg::transpose(m, o);
  //viennacl::linalg::transpose(m);
  std::cout << "m: " << m << std::endl;
  std::cout << "o: " << o << std::endl;

  std::cout << "---------------------" << std::endl;

  /**
  *  Compute the FFT using the Bluestein algorithm (usually faster, but higher memory footprint)
  **/
  std::cout << "Computing FFT bluestein" << std::endl;
  // Print the vector
  std::cout << "input_vec: " << input_vec << std::endl;
  std::cout << "Done" << std::endl;
  viennacl::linalg::bluestein(input_vec, output_vec, 0);
  std::cout << "input_vec: " << input_vec << std::endl;
  std::cout << "output_vec: " << output_vec << std::endl;
  std::cout << "---------------------" << std::endl;

  /**
  *  Computing the standard radix-FFT for a vector
  **/
  std::cout << "Computing FFT " << std::endl;
  // Print the vector
  std::cout << "input_vec: " << input_vec << std::endl;
  std::cout << "Done" << std::endl;
  viennacl::fft(input_vec, output_vec);
  std::cout << "input_vec: " << input_vec << std::endl;
  std::cout << "output_vec: " << output_vec << std::endl;
  std::cout << "---------------------" << std::endl;

  /**
  *  Computing the standard inverse radix-FFT for a vector
  **/
  std::cout << "Computing inverse FFT..." << std::endl;
  //viennacl::ifft(output_vec, input_vec); // either store result into output_vec
  viennacl::inplace_ifft(output_vec);     // or compute in-place
  std::cout << "input_vec: " << input_vec << std::endl;
  std::cout << "output_vec: " << output_vec << std::endl;
  std::cout << "---------------------" << std::endl;

  /**
  *  Convert a real vector to an interleaved complex vector and back.
  *  Entries with even indices represent real parts, odd indices imaginary parts.
  **/
  std::cout << "Computing real to complex..." << std::endl;
  std::cout << "input_vec: " << input_vec << std::endl;
  viennacl::linalg::real_to_complex(input_vec, output_vec, input_vec.size() / 2); // or compute in-place
  std::cout << "output_vec: " << output_vec << std::endl;
  std::cout << "---------------------" << std::endl;

  std::cout << "Computing complex to real..." << std::endl;
  std::cout << "input_vec: " << input_vec << std::endl;
  //viennacl::ifft(output_vec, input_vec); // either store result into output_vec
  viennacl::linalg::complex_to_real(input_vec, output_vec, input_vec.size() / 2); // or compute in-place
  std::cout << "output_vec: " << output_vec << std::endl;
  std::cout << "---------------------" << std::endl;

  /**
  *  Point-wise multiplication of two complex vectors.
  **/
  std::cout << "Computing multiply complex" << std::endl;
  // Print the vector
  std::cout << "input_vec: " << input_vec << std::endl;
  std::cout << "input2_vec: " << input2_vec << std::endl;
  viennacl::linalg::multiply_complex(input_vec, input2_vec, output_vec);
  std::cout << "Done" << std::endl;
  std::cout << "output_vec: " << output_vec << std::endl;
  std::cout << "---------------------" << std::endl;

  /**
  *  That's it.
  **/
  std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
  return EXIT_SUCCESS;
}