Need help with counting within parallelized block - OpenMP/C-Programming


 
Thread Tools Search this Thread
Top Forums Programming Need help with counting within parallelized block - OpenMP/C-Programming
# 8  
Old 10-10-2014
I should also note that for a problem this small, a single-threaded loop over 100 strings may be just as fast or faster than launching 100 tiny short-lived threads. There is overhead to starting and quitting a parallel operation. So, having a loop inside the thread so each thread does more work before it quits could be quite efficient. Give each thread a starting point and let it check 100 perhaps.
# 9  
Old 10-10-2014
Yes, I have seen a control in one lib called 'eureka', so the first thread to find a solution stops all other threads. The trick is to find a signal or interrupt to alert them, so all threads are not polling some variable to decide to continue.
# 10  
Old 10-10-2014
Quote:
Originally Posted by Corona688
I should also note that for a problem this small, a single-threaded loop over 100 strings may be just as fast or faster than launching 100 tiny short-lived threads. There is overhead to starting and quitting a parallel operation. So, having a loop inside the thread so each thread does more work before it quits could be quite efficient. Give each thread a starting point and let it check 100 perhaps.
Okay. Smilie Thank you for the information.

I've tried to parallelize your code. I must say, that I like it really much.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>

const char         Literals[]    = "0123456789";
const unsigned int NLiterals     = sizeof( Literals ) - 1;
const char         TestString[]  = "4877";
char               EndString[16];

void parallel_function( int, int * );

int main()
{
    int flag = -1;
    int count = 0;

    #pragma omp parallel default( none ) shared( flag ) reduction( +:count )
    {
        while( flag < 0 )
        {
            parallel_function( count, &flag );
            count++;
        }
    }

    if( !flag )
        printf( "Nothing found!!! %i tries\n", count );

    else
        printf( "Match: %s ! Tries: %i\n",  EndString, count );

    return 0;
}

void parallel_function( int count, int *flag )
{
    char str[10];
    int  c = count;

    if( ( *flag ) >= 0 )
        return;

    memset( str, 0, sizeof( str ) );

    // Compute the string from the value of 'c'. Each different
    // count will give a unique solution.
    int length = ( c % 5 ) + 1;
    c /= 5;

    for( int n = 0; n < length; n++ )
    {
        str[n] = Literals[c%NLiterals];
        c /= NLiterals;
    }

    // If and only if we have the correct solution, store the value.
    if( strncmp( TestString, str, sizeof( TestString ) ) == 0 )
    {
        strncpy( EndString, str, strlen( str ) );
        ( *flag ) = count;
    }
}

But unfortunately I have still the same behavior with the counting of the generated words. I think I have to learn more about parallelizing with OpenMP. Otherwise I'll never succeed with my small project. It's really difficult.

@Corona688: I thank you very very much for your help and for your code example. SmilieSmilie

---------- Post updated at 10:44 PM ---------- Previous update was at 10:42 PM ----------

Quote:
Originally Posted by DGPickett
Yes, I have seen a control in one lib called 'eureka', so the first thread to find a solution stops all other threads. The trick is to find a signal or interrupt to alert them, so all threads are not polling some variable to decide to continue.
Hello Smilie. Thank you for this information. Maybe this could be the solution. Okay, I have seen 'eureka' is as of OpenMP version 4.0.

Last edited by DaveX; 10-10-2014 at 06:22 PM..
# 11  
Old 10-10-2014
Quote:
Originally Posted by DaveX
But unfortunately I have still the same behavior with the counting of the generated words.
Why are you printing count? Why do you even have count? Print flag! That's what it's there for.

Finally, it's unavoidable when doing a parallel search that you may run more threads than strictly necessary. If you ran 4 simultaneous threads but found it on the very first try, that's tough luck, you ran 4 anyway.

Last edited by Corona688; 10-10-2014 at 07:08 PM..
This User Gave Thanks to Corona688 For This Post:
# 12  
Old 10-10-2014
Quote:
Originally Posted by Corona688
Why are you printing count? Why do you even have count? Print flag! That's what it's there for.
Yes. SmilieSmilieSmilie Sorry, after I read your post I changed it and it works!!! Oh man, this is really amazing. I don't know how to thank you. The number of counts is absolutely correct. Smilie
I can't believe it. Since weeks I'm searching for a solution. And you gave it to me. Smilie I can't say it often enough -> thank you. Smilie
The working source code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>

const char         Literals[]    = "0123456789";
const unsigned int NLiterals     = sizeof( Literals ) - 1;
const char         TestString[]  = "1";
char               EndString[16];

void parallel_function( int, int * );

int main()
{
    int flag = -1;
    int count = 0;

    #pragma omp parallel default( none ) shared( flag ) reduction( +:count )
    {
        while( flag < 0 )
        {
            parallel_function( count, &flag );
            count++;
        }
    }

    if( !flag )
        printf( "Nothing found!!! %i tries\n", flag );

    else
        printf( "Match: %s ! Tries: %i\n",  EndString, flag );

    return 0;
}

void parallel_function( int count, int *flag )
{
    char str[10];
    int  c = count;

    if( ( *flag ) >= 0 )
        return;

    memset( str, 0, sizeof( str ) );

    // Compute the string from the value of 'c'. Each different
    // count will give a unique solution.
    int length = ( c % 5 ) + 1;
    c /= 5;

    for( int n = 0; n < length; n++ )
    {
        str[n] = Literals[c%NLiterals];
        c /= NLiterals;
    }

    // If and only if we have the correct solution, store the value.
    if( strncmp( TestString, str, sizeof( TestString ) ) == 0 )
    {
        strncpy( EndString, str, strlen( str ) );
        ( *flag ) = count;
    }
}

At least I want to post my source code. Because through the kindness of Corona688, now my code works, too. Smilie
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>

/* Global variables */

const char         Literals[]    = "0123456789";
const unsigned int NLiterals     = sizeof( Literals ) - 1;
const char         TestString[]  = "4877";
char               EndString[16];

/* Functions */

static void BruteForcer( const int *, const int *, int, unsigned *, int * );

int main()
{
    unsigned Counter     = 0;
    int      CheckStatus = -1;

    #pragma omp parallel default( none ) shared( CheckStatus ) reduction( +:Counter )
    {
        for( int WordLength = 0; WordLength < 5; ++WordLength )
        {
            int FirstEntry[WordLength], LastEntry[WordLength];

            for( int j = 0; j < WordLength; ++j )
                FirstEntry[j] = LastEntry[j] = 0;

            for( int i = 0; i < NLiterals; ++i )
            {
                FirstEntry[0] = i;
                LastEntry[0]  = i + 1;

                if( CheckStatus < 0 )
                    BruteForcer( FirstEntry, LastEntry, WordLength, &Counter, &CheckStatus );
            }
        }
    }

    if( CheckStatus < 0 )
        fprintf( stdout, "\nNothing found: '%s'! %u tries.\n", "Empty", Counter / 8 ); // Divided through the number of cores/threads <- in my case eight (Intel Core i7-920) ;-)

    else
        fprintf( stdout, "\nWord found: '%s'! %u tries.\n", EndString, CheckStatus );

    exit( EXIT_SUCCESS );
}

static void BruteForcer( const int *FstEntry, const int *LstEntry, int WdLength, unsigned *CounterPtr, int *CheckStatusPtr )
{
    char Word[WdLength];
    int  Entry[WdLength + 1];
    int  i, j;

    memset( Entry, '\0', WdLength );

    /* copy FstEntry to Entry */
    for( i = 0; i < WdLength; ++i )
        Entry[i] = FstEntry[i];

    i = 0;

    while( i < WdLength )
    {
        ++*CounterPtr;

        /* generate word */
        for( i = 0; i < WdLength; ++i )
            Word[i] = Literals[Entry[i]];

        /* null-byte at end of string */
        Word[WdLength] = '\0';

        /* string compare */
        if( strncmp( TestString, Word, sizeof( TestString ) ) == 0 )
        {
            strncpy( EndString, Word, strlen( Word ) );
            *CheckStatusPtr = *CounterPtr;
            return;
        }

        /* increment Entry */
        for( i = 0; i < WdLength && ++Entry[WdLength-i-1] == NLiterals; i++ )
            Entry[WdLength-i-1] = 0;

        /* when Entry != LstEntry then leave loop */
        for( j = 0; j < WdLength; ++j )
            if( Entry[j] != LstEntry[j] )
                break;

        /* when Entry == LstEntry leave function */
        if( j == WdLength )
            return;
    }
}

The problem is solved. The thread can be closed. THANK YOU!!! Smilie

Last edited by DaveX; 10-10-2014 at 08:26 PM..
This User Gave Thanks to DaveX For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

OpenMP and MPI hybrid thread numbers

hi, I have two basic questions, I will be really grateful if I receive any comment from you, I have an MPI code where I am trying to implement OpenMP directives. The machine where I run the code has 16 cores. I run the code with export OMP_NUM_THREADS=2 mpirun -np 4 ./exec If I... (0 Replies)
Discussion started by: armando_2011
0 Replies

2. Programming

Undefined reference to omp_get_thread_num using OpenMP?

I am using a large code-base that compiled successfully before using make with a makefile and cmake. However, now that I'm trying to use openmp with it, I'm now getting the errors undefined reference to `omp_get_thread_num' undefined reference to `omp_get_num_threads'I don't think this... (0 Replies)
Discussion started by: larry burns
0 Replies

3. Programming

Gcc openmp programming problem

Dear Linux users, I'm a noob at openmp, gcc and c programming. I can run my own openmp code in terminal with no problem, eg. gcc -fopenmp program.c -o program. But now I'm trying to compile and run another person's code, it contains a makefile and multiple .c and .h files. I don't know how to... (2 Replies)
Discussion started by: pigeon151
2 Replies

4. Programming

Building Block style programming Book

Hello to all, Here is my situation. Some time in the mid-80's I stumbled across a small white programming book - can't remember the name but it was unique in that it started right out giving instructions on creating building blocks in code as a foundation for a complete system. The book was... (2 Replies)
Discussion started by: jozefn
2 Replies

5. HP-UX

Spawn more than 8 threads OpenMP & HPUX

Hi folks, I am trying to run more than 8 threads in OpenMP team on my HP-UX 11i v3 system (without root access), but NO success. Compiler: aCC A.06.26 I tried to setup: OMP_NUM_THREADS, omp_set_num_threads(), max_thread_proc=1000, nkthread=8416, set_dynamic=0 Machine has 2 processors... (1 Reply)
Discussion started by: ATveretinov
1 Replies

6. Shell Programming and Scripting

Counting lines in each block file

hello im new here so i want to say hi everybody :) i have to write a script and im newbie :/ i hope that in this forum are many ppl who knows subject :) i have hundrets folders. in each folder is a file name trace.txt. each trace.txt has a lot of tracert's results separates with "-----" it... (6 Replies)
Discussion started by: michael8484
6 Replies

7. UNIX for Dummies Questions & Answers

gzip parallelized

Hello everyone, I've got a question regarding the gzip command. I regulary use gzip to pack huge ammounts of files. Is it ok to start 'gzip *' several times in the same directory to parallelize the packing process or can this result in problems, e.g. broken or unpacked files? My tests... (7 Replies)
Discussion started by: Basch
7 Replies

8. UNIX for Dummies Questions & Answers

Carreer:Networking Programming in Unix (C programming Language)

Hello, I am trying to learn Networking Programming in C in unix enviorment. I want to know how good it is to become a network programmer. i am crazy about Network programming but i also want to opt for the best carreer options. Anybody experienced Network Programmer, please tell me is my... (5 Replies)
Discussion started by: vibhory2j
5 Replies
Login or Register to Ask a Question