Search in the blog

вівторок, 23 листопада 2010 р.

Here is "False sharing" example, you can try it in VS with enabled OpenMP flag. Also if you enable speed optimization for compiler single thread version will be faster, but you can see false sharing effect too.

#include <iostream>
#include <ctime>
#include <omp.h>

using std::cout;
using std::endl;

const double M=8e8;

//single thread work to do
void one_thread() 
{
    long int n1=1, n2=1;
    for(; n1<M; n1+=n1%3);
    for(; n2<M; n2+=n2%3);
    cout << "Sum = " << n1 + n2 << endl;
}


//multi thread work to do
void multi_thread() 
{
    struct T
    {
        long int n1;
        char chuck[64];//should be commented to see false sharing effect
        long int n2;
    } t;
    t.n1 = 1;
    t.n2 = 1;
    #pragma omp parallel num_threads(2) shared(M,t)
    {
        #pragma omp sections
        {
            #pragma omp section
            for(; t.n1<M; t.n1+=t.n1%3);

            #pragma omp section
            for(; t.n2<M; t.n2+=t.n2%3);
        }
    }
    cout << "Sum = " << t.n1 + t.n2 << endl;
}

time_t start, end;
double diff;

int main(int argc, char* argv[]) 
{
    time(&start);
    multi_thread();
    time(&end);
    diff=difftime(end, start);
    cout<<diff<<" seconds elapsed for multi thread calculation."<<endl;

    time(&start);
    one_thread();
    time(&end);
    diff=difftime(end, start);
    cout<<diff<<" seconds elapsed for 1 thread calculation."<<endl;
    return 0;
}

четвер, 12 серпня 2010 р.

Again about rvalues

You can use rvalues for creating move semantics - remove copy constructors for temporaries. Example:

#include <iostream>

class A
{
public:
    A()
    {
        x = new int[1];
        x[0] = 1;
        std::cout << "new\n";
    }
    ~A()
    {
        delete[] x;
    }

    A(const A& a)
    {
        x = new int[1];
        x[0] = a.x[0];
        std::cout << "new\n";
    }

   /* A(A&& a) //move constructor
    {
        x = a.x;
        a.x = 0;
    }*/
private:
    int *x;
};

A GetA()
{
    A a;
    return a;
}

//if you want to use movable object as member in another object you should declare move constructor
//and use std::move
class B
{
public:
    B(){}
    B(B&& b) //move constructor
    {
        a = std::move(b.a);
    }
    A a;
};

B GetB()
{
    B b;
    return b;
}

int main(int, char*[])
{
    B b = GetB();
    {
        A a = GetA();
        A a2(a);
        std::cout << "scope\n";
    }
    return 0;
}

понеділок, 7 червня 2010 р.

New web page

I have updated my personal page. You can find updated demo section there.

четвер, 3 червня 2010 р.

C++0x multi-threading

If you want to play with new functionality for multi-threading in C++ for free you need gcc-4.5 and Linux, MinGW currently doesn't support this functionality (but it has all required headers). To compile programm you need to specify complier flag -std=c++0x and linker flag -pthread. VC++ currently have not this functionality too. Here is small example:

#include <thread>
#include <mutex>
#include <iostream>
#include <unistd.h>

class MyJob
{
public:
    MyJob(std::mutex* m) : m(m){}
    void operator()()
    {
        for (int i = 0; i < 5; ++i)
        {
            {
                std::lock_guard<std::mutex> lock(*m);
                std::thread::id id = std::this_thread::get_id();
                std::cout << "Thread " << id << std::endl;
            }
            sleep(1);
        }
    }
private:
    std::mutex* m;
};

int main()
{
    std::mutex m;
    std::thread t1{MyJob(&m)};
    std::thread t2{MyJob(&m)};

    t1.join();
    t2.join();

    return 0;
}

четвер, 13 травня 2010 р.

FLTK Fluid horisontal widgets

If you want to make some widget in FLTK horizontal you have to go to the c++ properties:

вівторок, 27 квітня 2010 р.

CMake + VC++2010

There is now the bug in CMake 2.8.1 with Visual Studio 2010 Express - compiler checks doesn't work. Here is http://www.itk.org/Bug/view.php?id=10539 patch for this bug.

вівторок, 20 квітня 2010 р.

Time convertions with daylight saving

I've found how to convert time from UTC to local time with daylight saving correction:
1. Use mktime() to create time from string(or some thing else)
2. get time from gmtime() and localtime()
3. calculate difference between time to get timezone offset (or use _get_timezone)
4. use tm::tm_isdst to determine apply or not daylight saving
5. use _get_daylight() to get value in hours of daylight correction.