Math Is Fun Forum
  Discussion about math, puzzles, games and fun.   Useful symbols: √ ∞ ≠ ≤ ≥ ≈ ⇒ ∈ Δ θ ∴ ∑ ∫ π -

Login

Username

Password

Not registered yet?

Post a reply

Go back

Write your message and submit
:) :| :( :D :o ;) :/ :P :lol: :mad: :rolleyes: :cool: | :dizzy :eek :kiss :roflol :rolleyes :shame :down :up :touched :sleep :wave :swear :tongue :what :faint :dunno
Options

Go back

Topic review (newest first)

bobbym
2013-01-10 03:39:54

Hi;

That is weird. Did you try to use the code I provided?

Giewont
2013-01-10 03:32:12

bobbym wrote:

Hi;

What was the error message the compiler gave?

There aren't compiler's errors, but the application produces bad, meaningless results.
(completely different than non-parallel version) sad

bobbym
2013-01-08 05:53:27

Hi;

What was the error message the compiler gave?

Giewont
2013-01-07 22:21:28

I found a solution:

Code:

    int k, i, j;
    for (k=0; k<3; k++)
   {
      r[k][k]=0; // equivalent to sum = 0
      for (i=0; i<3; i++)
        r[k][k] = r[k][k] + a[i][k] * a[i][k]; // rkk = sqr(a0k) + sqr(a1k) + sqr(a2k) 
      r[k][k] = sqrt(r[k][k]);  // ||a||
      
      for (i=0; i<3; i++)
          q[i][k] = a[i][k]/r[k][k];
     
      for(j=k+1; j<3; j++) {
        r[k][j]=0;
        for(i=0; i<3; i++) r[k][j] += q[i][k] * a[i][j];
        for (i=0; i<3; i++) a[i][j] = a[i][j] - r[k][j]*q[i][k];
        
      }

I have tested this and it's working good.
But now I must parallelize this code using OpenMP (it's the project for my college) and
I have a new problem. I try parallelize the main "for":

Code:

int k, i, j;
#pragma omp parallel for private (k, i, j) shared (a, q, r)
// ........

but it doesn't work correctly. I noticed that the problematic fragment is:

Code:

for (i=0; i<3; i++) 
          q[i][k] = a[i][k]/r[k][k];

but I don't know why it makes a problem...

Anybody help ?

bobbym
2012-12-26 04:50:16

Hi;

I need a little bit more than that.

Code:

#include <iostream>
#include <glm/glm.hpp>

glm::vec3 sum_over_e(glm::vec3* e, glm::vec3* e_prime, int& i)
{
    int k = 0;
    glm::vec3 result;

    while (k < i-1)
    {
        float e_prime_k_squared = glm::dot(e_prime[k], e_prime[k]);
        result += ((glm::dot(e[i], e_prime[k]) / e_prime_k_squared) * e_prime[k]);
        k++;
    }

    return result;
}

int main(int argc, char** argv)
{
    int n = 3;  // number of vectors we're working with
    glm::vec3 e[] = {
        glm::vec3(sqrt(2)/2, sqrt(2)/2, 0),
        glm::vec3(-1, 1, -1),
        glm::vec3(0, -2, -2)
    };

    glm::vec3 e_prime[n];
    e_prime[0] = e[0];  // step A

    int i = 0;  // step B

    do  // step C
    {
        e_prime[i] = e[i] - sum_over_e(e, e_prime, i);

        i++;    // step D
    } while (i < n);

    for (int loop_count = 0; loop_count < n; loop_count++)
    {
        std::cout << "Vector e_prime_" << loop_count+1 << ": < " 
                  << e_prime[loop_count].x << ", " 
                  << e_prime[loop_count].y << ", " 
                  << e_prime[loop_count].z << " >" << std::endl;
    }

    return 0;

That is supposed to orthogonalize those three vectors using Gram Schmidt. I have not tried it but it is supposed to work.

I must point out that the above method can be numerically unstable and the modified method will produce better results.

Giewont
2012-12-26 04:49:02

Hello !

I'm very beginner in numerical methods... I have the pseudo-code
(look at image), but I don't know how intrepret this and how
I can implement this in C++...

Can You help me ?

Board footer

Powered by FluxBB