Showing posts with label Computational Science. Show all posts
Showing posts with label Computational Science. Show all posts

Sunday, June 9, 2013

Array of Vectors using Thrust

thrust::device_vector<float> vectors[3];
//thrust::device_vector<float> *vectors = new thrust::device_vector<float>[3];

vectors[0] = thrust::device_vector<float>(10);
vectors[1] = thrust::device_vector<float>(10);
vectors[2] = thrust::device_vector<float>(10);

Generating a random number vector between 0 and 1.0 using Thrust

http://stackoverflow.com/questions/12614164/generating-a-random-number-vector-between-0-and-1-0-using-thrust


#include <thrust/random.h>
#include <thrust/device_vector.h>
#include <thrust/transform.h>
#include <thrust/iterator/counting_iterator.h>
#include <iostream>

struct prg
{
    float a, b;

    __host__ __device__
    prg(float _a=0.f, float _b=1.f) : a(_a), b(_b) {};

    __host__ __device__
        float operator()(const unsigned int n) const
        {
            thrust::default_random_engine rng;
            thrust::uniform_real_distribution<float> dist(a, b);
            rng.discard(n);

            return dist(rng);
        }
};


int main(void)
{
    const int N = 20;

    thrust::device_vector<float> numbers(N);
    thrust::counting_iterator<unsigned int> index_sequence_begin(0);

    thrust::transform(index_sequence_begin,
            index_sequence_begin + N,
            numbers.begin(),
            prg(1.f,2.f));

    for(int i = 0; i < N; i++)
    {
        std::cout << numbers[i] << std::endl;
    }

    return 0;
}

Monday, May 20, 2013

Natural Units

http://en.wikipedia.org/wiki/Natural_units

Plank Unit

"Natural units" (particle physics) [edit]

UnitMetric valueDerivation
1 eV−1 of length1.97×10−7 m=(1\text{eV}^{-1})\hbar c
1 eV of mass1.78×10−36 kg= (1 \text{eV})/c^2
1 eV−1 of time6.58×10−16 s=(1\text{eV}^{-1})\hbar
1 eV of temperature1.16×104 K= 1 \text{eV}/k_\text{B}
1 unit of electric charge
(L–H)
5.29×10−19 C=e/\sqrt{4\pi\alpha}
1 unit of electric charge
(G)
1.88×10−18 C=e/\sqrt{\alpha}
In particle physics, the phrase "natural units" generally means:[4][5]
 \hbar = c = k_\text{B} = 1.
where \hbar is the reduced Planck constantc is the speed of light, and kB is the Boltzmann constant.
Like the other systems (see above), the electromagnetism units in Planck units can be based on either Lorentz–Heaviside units or Gaussian units. The unit of charge is different in each.
Finally, one more unit is needed. Most commonly, electron-volt (eV) is used, despite the fact that this is not a "natural" unit in the sense discussed above – it is defined by a natural property, the elementary charge, and the anthropogenic unit of electric potential, the volt. (The SI prefixed multiples of eV are used as well: keV, MeV, GeV, etc.)
With the addition of eV (or any other auxiliary unit), any quantity can be expressed. For example, a distance of 1 cm can be expressed in terms of eV, in natural units, as:[5]
1\, \text{cm} = \frac{1\, \text{cm}}{\hbar c} \approx 51000\, \text{eV}^{-1}

Monday, April 29, 2013

Sunday, April 28, 2013

Passing Objects by Reference and By Pointer

http://people.cs.vt.edu/~kafura/cs2704/reference.html


??


   window.Display(box1);  // uses by reference
   window.Display(box2);  // uses by pointer
   window.Display(&box3);  // uses by pointer
   window.Display(*box4);  // uses by referen


http://stackoverflow.com/questions/410593/pass-by-reference-value-in-c

Pointer to C++ STL vector

http://stackoverflow.com/questions/6946217/pointer-to-a-vector

Fortran data type

http://en.wikibooks.org/wiki/Fortran/complex_types


If you want double-precision complex numbers, you're pretty much stuck with specifying a precision, and hoping that the compiler likes that format. Here's where portability comes in: If, for instance, the machine has floating point types of lengths 4, 8, and 16, and I specify a complex length of 8, 16, or 32, I'm likely to get a pair of floating point numbers at the size I expect. If, however, I specify a length of some other value, for instance, 10, then I'm likely to get a pair of 4s or 8s, depending on what the compiler likes to do. So, in general, to get the values as double-precision, I'd code:
 COMPLEX*16 myVariable, anotherVariable, anArray(2,3)

Saturday, April 27, 2013

iterating through 2d "vector" c++

http://stackoverflow.com/questions/1784573/iterator-for-2d-vector


Although your question is not very clear, I'm going to assume you mean a 2D vector to mean a vector of vectors:
vector< vector<int> > vvi;
Then you need to use two iterators to traverse it, the first the iterator of the "rows", the second the iterators of the "columns" in that "row":
//assuming you have a "2D" vector vvi (vector of vector of int's)
vector< vector<int> >::iterator row;
vector<int>::iterator col;
for (row = vvi.begin(); row != vvi.end(); row++) {
    for (col = row->begin(); col != row->end(); col++) {
        // do stuff ...
    }
}

Multi Dimensional Array

The more STL savvy coder will opt for a vector-of-a-vector approach to MD arrays, rather than Nested New:

1
2
3
4
// to make a [5][6] array:
vector< vector<int> > stillevil( 5, vector<int>(6) );

stillevil[2][3] = 1;


Not a lot to say about this one. Like Nested New it's incompatible with other MD array flavors. If you need to pass this array to a function, none of the above functions will work.

Also, like Nested New, it's inefficent in that is requires additional memory and has double indirection.

On the plus side, you don't need cleanup code, and the allocation code is not as ugly (but still pretty ugly!)


Wednesday, February 27, 2013

Wednesday, January 23, 2013