C :: Using Counter Variable With OpenMP

Oct 14, 2014

I'm trying to write a small brute force application in C on Ubuntu 14.04.1 using Code::Blocks with gcc-4.8.2. It's nothing special and just for practice.The focus is on a parallel procedure that generates strings by use of OpenMP. Every time a string is generated, the counter variable should be incremented. At the end, regardless of whether the search string was matched or not, it outputs the correct number of all generated strings (counts).It works fine so far.Here is my source code example:

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>
/* Global variables */
const char Literals[] = "abcdefghijklmnopqrstuvwxyz";
const int NLiterals = sizeof( Literals ) - 1; // NLiterals = 26 Bytes.
const char SearchString[] = "test"; // I.e. until 'test' there are 355414 counts.
char MatchString[16];
}

[code]...

This should speed up the process of generating strings. But if I do so, always when the program has a match the number of counted strings is wrong. Here are some possible "wrong" outputs: Code: Match: 'test'! 40710 tries. <-- number of tries (counts) is wrong! or Code: Match: 'test'! 40375 tries. <-- number of tries (counts) is wrong! But if the program doesn't have a match (i.e. when I change SearchString to something like the following line), then the number of counts is correct: Code: const char SearchString[] = "test0"; The only thing I have to do before, is to change the follow line from:

Code:

printf( "
No match! %u tries.
", Count / 8 ); to Code: printf( "
No match! %u tries.
", Count );

But also it can happen that there is no output. Like an infinite or endless loop. How can I improve the source code in order that the correct number of counts is always displayed and what can be the reason for the endless loops time after time? Both issues appears only by the use of Code: #pragma omp for schedule( dynamic ).

View 5 Replies


ADVERTISEMENT

C# :: How To Correctly Use Counter Variable In WHILE Loop

Feb 20, 2014

After searching, it seems that I've finally arrived at the good old traditional "Random Number Game". I've been presented with solving this problem in the context of Visual C#.

I've solved the first part of the problem: Have the user enter a random number, display "Too high" or "Too Low" depending on the entry. If user guesses, let the user know. That part of the problem I was able to solve with only 3 IF statements. In it's current version, instead of using all IF statements, I saw where 3 test conditional weren't needed. (If it's not greater nor lesser it's equal or,.... "otherwise do this").

An enhancement to the problem ask that the user be notified of how many guesses it took to get the number correct. I reasoned that number of guesses could be translated to mean " number of iterations ".

I'm having trouble using the counter variable to track the iterations, then display the total iterations. I'm not seeing how to increment that value with each loop. I've basically reached two results no matter how I've used counter and written the while loop. The first one returns back to me "the guessed number". Example, if I guessed 10 times and the lucky number was 58, the display statement in the ELSE clause became: "Congrats. You guessed the number in 58 tries!".

The other result is what's returned by the code in it's current form. I think it'll be clearer for me to debug from this point then the previous. Here, I can see how I reach "Congrats. You guessed the number in 2 tries!". It's apparent that counter is being incremented by one regardless how many guesses the user makes. On the contrary, it's not so apparent to me how previously, the value returned by the counter variable at the end of the loop was actually the value for the random number generated and the user's entry (hence, the correct number) and NOT the total loop iterations.

Here's the code :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

[Code] .....

View 4 Replies View Related

C/C++ :: Library Reserve System - Counter Variable?

Sep 15, 2014

I am writing a program that minicks the library reserve system at my university and the problem is that when i run the code and reserve a room i am to keep track of the amount of times the room has been visited. To do this i set up a array roomcount and I count up by one every time i assign a room, The bug lies with when you reserve a room, then run option three, it will show that the previous visits is at 1, which is good. but then if i leave the room(option 2) and then run option 3 again to view the status of the rooms, the previous visits is set back to 0, which is not good. Ive been staring at this for hours and asked numerous people by we can't seem to figure out why the roomcount variable resets.

#include <stdio.h>
#include <math.h>
int main (void) {
int choice;
int roomchoice;
int i = 0;

[Code] .....

View 4 Replies View Related

C/C++ :: Defining Class Counter - Member Variable Should Be Private

Oct 23, 2014

The question is: Define the class Counter. An instance of this class is used to count things, but the counter should never be less than 0 (non negative number). The member variable should be private. I realize what I'm suppose to be using but can't implement the member functions needed..

int main(){
int value;
cin >> value;
Counter myCounter(value);
for (int i = 1; i <= MAXLOOP; i++) {
myCounter.increment();

[Code] ....

View 3 Replies View Related

C :: Threading With OpenMP

Apr 10, 2013

I wrote code that finds the number of prime numbers in a range entered by the user. Now I'm attempting to make it run in parallel with the number of threads I assign it to have. I'm using blocking technique, so I'm assigning, in this scenario, 4 threads - 1/4 of the numbers in the range to the first array, and the next 1/4 of the numbers in the range to the next array and so on. Then I want to execute the prime number counting code in parallel. I'm using openMP to do this. I'm having difficulty setting it all up properly. I have a little experience with pthreads but little with openMP and am struggling in how this should be done.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

[Code]....

View 2 Replies View Related

C/C++ :: Getting Error While Using OpenMP Along With Map

Dec 19, 2012

I am using map for assigning an identifiers for a line from file in c++. Also i am using IDs for a pattern in line {name, operator, val} which is separated by ';'. I am using read_pub() for calculating how many patterns are there in a line.
 
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<fstream>
#include<omp.h>
#include<map>
#include <sstream>  
using namespace std;
struct predicate {

[Code] ....

View 1 Replies View Related

C :: Translate Pthreads Into OpenMP

May 20, 2013

I'm quite new to openMP, mostly used pthreads and mpi before. Now I like to tinker a bit with openMP, but haven't found any good docs, reference list or similar.

What's the equivalent to pthread's mutex lock in openMP?

Code:
#pragma omp parallel for
for(i=0; i<n ; i++){
// Do something intelligent...
// If needed handle a shared variable.
}

How do I protect the shared variable?

View 1 Replies View Related

C++ :: OpenMP Parallel Is Not Working

May 22, 2013

I'm trying to implement Parallel loop with OpenMP. I just googled and got the below sample, but it seems its not executing parallely. It takes same amount time for code which is with parallel and without parallel.

Without Parallel : It takes 39 secs.
-----------------
for (int I=0;I<100000;I++){
cout<<I<<" ";
}

With Parallel : It takes 39 secs.
--------------

#include<omp.h>
#pragma omp parallel {
#pragma omp for
for (int I=0;I<100000;I++) {
cout<<I<<endl;
}
}

Why parallel is not working and it takes same time. My machine is Dual Core.

View 4 Replies View Related

C++ :: OpenMP - Intervals Assigned To Threads?

Feb 6, 2014

Is there a way of knowing which indices a thread is assigned in a parallel openMP scope?

View 6 Replies View Related

C++ :: Using One (dedicated) Vector Per Thread (OpenMP)

Apr 11, 2013

I recently picked up multithreading with OpenMP, so it is still new to me. While processing data, I would like to store it in a vector, as shown here:

vector<int> vect;
#pragma omp parallel for
for(int c=0; c<500; ++c) {
vect.push_back(c)
}

However, since it is multithreaded, I would like (and need!) to have one dedicated vector per thread. I haven't been able to find an example of how to do this online. Is it even possible?

View 1 Replies View Related

C/C++ :: OpenMP Can Make For Loop Faster?

Dec 9, 2014

I'm trying to optimize this code using openMP. The line I added made it run about twice as fast but I'm trying to figure out how to make it even faster. Could reshaping the loops potentially increase the speed?

//4 threads
int listsize=15000;
#pragma omp parallel for shared(f) private(i,j,hash)
for(i = 0; i < listsize; i++) {
printf("Thread: %d i: %zu wl_size: %zu
",omp_get_thread_num(),i,wl_size);
for (j = 0; j < num; j++) {
h = f[j] (getw(list, i));
c[h] = 1;
} }

View 2 Replies View Related

C :: GSL And OpenMP - Getting Random Numbers That Are Out Of Expected Range

May 2, 2013

I am trying to parallelize some of my code with OpenMP. When I switch to using multiple threads I start getting random numbers that are out of the expected range.

Here is a small example:

Code:
#include <stdio.h>
#include </usr/local/include/gsl/gsl_rng.h>
#include </usr/local/include/gsl/gsl_randist.h>
int main() {
int mySeed=0;
const gsl_rng_type *T;
T = gsl_rng_ranlxs2;
gsl_rng *r_1 ;

[Code] .....

gsl_rng_uniform should only output number in the range [0,1) but with multiple threads it outputs larger number as well.

View 2 Replies View Related

C++ :: How To Add A Counter For Comparisons

Apr 27, 2013

How do I add a counter for the comparisons so that it increments every time the two array value are compared?

// Session5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
const int NMAX =10;

[code].....

View 1 Replies View Related

C# :: Counter Won't Reset To 0

Jul 30, 2014

int counter won't reset to 0 when I click the start over button.

protected void startOverButton_Click(object sender, EventArgs e)
{
gameOverLabel.Visible = false;

[Code]....

View 3 Replies View Related

C/C++ :: Why Counter Keep Resetting To Zero

Feb 17, 2015

I am not sure why this is happening but I am simply trying to make rounds with my code and have it stop when one of two things, or both, happen.

#include <iostream>
#include "Classes.h"
#include "RNG.h"
#include "select.h"
#include "Fitnesschk.h"
using namespace std;
/*Global Variables************************************/
const int column = 32;

[Code] ....

I've tried resetting and renaming all variables but I don't see any conflict. I tried declaring in different areas but no dice. The other issue I have is that fittest always returns 0 even if the function returns 1. So it infinitely loops. When I go through looking at the variables. the fittest variable never changes even if the function returns 1 and round seems to reset after reproduce is called.

Here is the supporting code.

For Fittracker:

int fittracker(person man[], int row, int column){
for (int y = 0; y < row; y++){
int fit = 0;
for (int z = 0; z < column; z++){
if (man[y].data[z] == 1){

[Code] ....

View 10 Replies View Related

C :: How To Make A Char Counter

Apr 14, 2013

One that fills different char arrays the idea is to use a single char to represent a char such as 255 but if more than it then use a char array of two chars to represent the next char 00 because if only one is needed it would take less disk space than 0,255 as it would in an array.

My goal is to do this all the way up to an array that can hold 255,255,255,255 but only use the more complex array if needed Example: a char of 255 takes much less space than a char array of 4 chars so that instead of 0,0,0,255 it is written to disk as a single char whereas a char array of 200,0,0,1 would be written as a 4 byte char array. use single char to represent values from 0 to 255 use two char array to represent 0,1 to 255,255 three for 0,0,1 to 255,255,255 and four for 0,0,0,1 to 255,255,255,255

View 3 Replies View Related

C :: Unique Word Counter

Jun 18, 2013

I am trying to make a program which reads a text file, separates the strings into words, then finally saving and counting each different word.

When I comment out the code where it is supposed to search for the same word in the table, the program doesn't work.Am I doing something wrong with strcmp or the loop? The 'break' just exits out of that 'for' loop right?

Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define WORD_MAX 500
#define LIST_MAX 500
int main(){
int i, j, find;

[Code]...

View 6 Replies View Related

C++ :: Counter-controlled While Loop?

Jan 2, 2014

The program should be print the numbers 0 through 10, along with their values doubled and tripled. The data file contains the necessary variable declarations and output statements.

Example the output:

Single Double Triple
1 2 3
2 4 6
3 6 9
4 8 12

here my code tell me if correct

#include <iostream>
#include <cstdlib>
using namespace std;
int main () {
int x, double, triple;

[Code] .....

View 1 Replies View Related

C/C++ :: 3 Led Output Binary Counter

Oct 4, 2014

I need to get up to speed in Embedded systems. I need to create a 3led binary counter, when an input is operated the it needs to count up using these leds from 0 to 7. I have written this so far and how to pulse a counter or interger to 7 and then reset back to 0.

int main (void) {
LED_Init_1();
LED_Init_2();
LED_Init_3();

[Code]......

View 2 Replies View Related

C/C++ :: Prime Number Counter?

Feb 23, 2014

I have to write a program that asks the user to input a positive integer, counts how many primes there are before this integer, then divide the number of primes by the integer. Here's what I have so far:

int n;
int counter = 0;
cout<<"Enter a positive integer n.";
cin>>n;

[Code]....

the value of the integer counter is supposed to increase whenever a prime number is identified but it's increasing n times

View 8 Replies View Related

C/C++ :: Parenthesis Counter Using Stack

Jul 26, 2014

I am trying to make a parenthesis counter using stack in C. But my code is always returning that my expression is invalid.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#define SIZE 100
#define True 1
#define False 0
struct stack {
int top;
char items[SIZE];

[Code] ....

View 5 Replies View Related

C++ :: To Form Counter Of N Element

Jun 3, 2012

How can i form a counter of say 30 elements so that to meet following requirement,

I need to access 5 elements out of 30 elements and out of these 5 elements once the one value is printed (i.e now access for remaining 4 element will be left) other one should get added immediately to this 5 element set, from remaining total 25 elements. In this way 5 element count will be there always.

I am trying this as follows, by forming vector of 30 elements accessing its 5 elements as :

for (iter = vector1.begin(); it < vector1.end();it++) {
for(int count = 5; count >= 1 ; count--) {
cout << "Print value<" <<count<<"> : "<< *it <<endl;
it++;
}
}

This displays 5 elements at a time but counter "count" waits for to finish off its remaining 4 elements however i want to maintain this counter to 5 elements always.

View 1 Replies View Related

C# :: Performance Counter Raw Value Is Not Changed / Modified

Apr 6, 2015

I am trying to modify a PerformanceCounter I have created in C#. But it doesn't seem to be that it is being changed. This counter needs actualy to be a flag : 0 or 1.

I took the following code from the web. It created the collectors category along with the counters well. But the RawValue always shows 0!

I am working on Win7/64.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace PerformanceCounterSample

[Code] ....

View 8 Replies View Related

C# :: How To Repeat Execution Of Code Using Counter

Oct 22, 2014

I have a method that changes a canvas color after set intervals, e.g. start timer, 5 seconds green, 3 seconds red, then stop. This functionality is provided in the interval method. The problem I'm trying to achieve is getting this sequence to repeat for a set number of iterations.

I tried to solve this by setting up a counter after the timer is stopped but the code keeps repeating indefinitely by starting and stopping over and over instead of the max of 6 iterations I had set. In debugging the problem, I watched the value of 'i' and when the 'if' statement is set to false. The 'if' statement gets set to false after 7 iteration as expected but the start(); keeps getting called.

void myTimer_Tick(object sender, EventArgs e) {
//Assign text box string value to a time span variable.
TimeSpan workTm = TimeSpan.ParseExact(wrkString, @"hh : mm : ss : fff", CultureInfo.InvariantCulture);
TimeSpan restTm = TimeSpan.ParseExact(rstString, @"hh : mm : ss : fff", CultureInfo.InvariantCulture);

// update the textblock on the display
// with hh, mm, ss, ms
ms = myStopwatch.ElapsedMilliseconds;

[Code] .....

View 1 Replies View Related

C/C++ :: WebForms - Counter Won't Increment More Than Once Onclick

Jul 28, 2014

I have a counter that onclick should increment 1 and it does that on click, but if I click the button again, it won't increment again. Instead it will be stuck at 1. How can I make it go up if the button is clicked more than once? Also this is a web application.

protected void submitAnswerButton_Click(object sender, EventArgs e) {
int counter = 0;
if (mathAnswerTextBox.Text == answer.ToString()) {
answerStatus.Text = "Correct!";

[Code] ....

View 12 Replies View Related

C++ :: Error With Running Counter Through Loops

Jan 27, 2014

I'm working on a program that reads data in line by line from an input file and the sorts it into two arrays, one string, one float. They are parallel arrays managed by a running data count of how many lines are read in, however, I'm getting an error with my data count. My test file only has 8 lines but when I cout my data count, it's like 35,485 or something like that. Here's the code I have so far (my program still has other components to write but I haven't gotten that far.)

Code:
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;
//global constants
const int MAX = 1000;// maximum numbers of input for array size

[Code] .....

View 14 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved