Intel® TBB Code Samples Featured in 1-Minute Intro Videos

  • Intel TBB parallel_for
  • See video

    #include <stdlib.h>
    #include <cmath>
    #include "tbb/tbb.h"
    
    double *output;
    double *input;
    
    
    int main() {
        const int size = 20000000;
        output = new double[size];
        input = new double[size];
       
        for(int i = 0; i < size; i++) {
            input[i] = i;
        }
          
        tbb::parallel_for(0, size, 1, [=](int i) {
    
            output[i] = sqrt(sin(input[i])*sin(input[i]) + cos(input[i])*cos(input[i]));
                
       });
        return 0;
    }
  • Intel TBB concurrent_vector
  • See video

    #include <stdlib.h>
    #include <vector>
    
    #include "tbb/tbb.h"
    
    using namespace std;
    
    tbb::concurrent_vector<int> my_list;
    
    void add_element(int i) {
    
        my_list.push_back(i);
    
    }
    
    
    int main() {
        const int size = 100000;
          
        tbb::parallel_for(0,size,1, [=](int i) {
    
           add_element(i);
                
       } );
    
        return 0;
    }
  • Intel TBB concurrent_queue
  • See video

    #include <queue>
    #include <cstdio>
    #include "tbb/tbb.h"
    
    using namespace std;
    
    tbb::concurrent_queue< int > q;
    
    void fill_q() {
        for ( int i = 0; i < 10; ++i ) {
            q.push(i);
        }
    }
    
    void pop_elements() {
        int i = 0;
        while ( q.try_pop(i) ) {
           printf("Popped %dn", i);
       }
    }
    
    int main() {
       
        fill_q();
        pop_elements();
        return 0;
    }
  • Intel TBB malloc_proxy library
  • See video

    export LD_PRELOAD=libtbbmalloc_proxy.so.2

For more complete information about compiler optimizations, see our Optimization Notice.