#pragma omp parallel for. Where to use it?

This is a discussion on #pragma omp parallel for. Where to use it? within the C Programming forums, part of the General Programming Boards category; Code: void playBoss(){ int olddirection = 0, newdirection = 0; int bossDirection; int health = 500; //#pragma omp parallel for ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    20

    #pragma omp parallel for. Where to use it?

    Code:
    void playBoss(){	int olddirection = 0, newdirection = 0;
    	int bossDirection;
    	int health = 500;
    	
    	//#pragma omp parallel for private(newdirection){
    	do {
    		
    		bossDirection = rand() % 4;
    		if(bossDirection == 0)
    			newdirection = 0;
    		if(bossDirection == 1)
    			newdirection = 1;
    		if(bossDirection == 2)
    			newdirection = 2;
    		if(bossDirection == 3)
    			newdirection = 3;
    		/*if(boss[newdirection].xCoord == character[newdirection].xCoord || boss[newdirection].yCoord == character[newdirection].yCoord){
    			if(newdirection == 0){
    				newdirection = 4;}
    			else if(newdirection == 1){
    				newdirection = 5;}
    			else if(newdirection == 2){
    				newdirection = 6;}
    			else if(newdirection == 3){
    				newdirection = 7;}}*/
    			
    	
    	if(olddirection != newdirection){
    		boss[newdirection].xCoord = boss[olddirection].xCoord;
    		boss[newdirection].yCoord = boss[olddirection].yCoord;
    		
    		boss[olddirection].drawable = 0;
    		boss[newdirection].drawable = 1;
    		boss[newdirection].currentFrame = 0;
    		if(newdirection == 0 || newdirection == 1 || newdirection == 2 || newdirection == 3){
    		olddirection = newdirection;}
    	}
    #pragma omp parallel for
    	
    	for(int k = 0; k<20; k++){
    	boss_animation();
    	if(boss[newdirection].xCoord >36 || boss[newdirection].xCoord < 733 || boss[newdirection].yCoord > 60 || boss[newdirection].yCoord < 682){
    		newdirection++;
    		Sleep(10);}
    	check_boss();}
    	
    	}
    	while(health > 0);
    }
    
    
    void boss_animation(){
    	int area = imagesize(0, 0, 799, 799);
    	bkimage = malloc(area);
    	getimage(0, 0, 799, 799, bkimage);
    	
    	#pragma omp parallel for
    		
    	for (int i = 0; i < 3; i++){
    		if( ! boss[i].drawable)
    			continue;
    	//Puts image of character on screen based on inputs from struct
    	readimagefile(boss[i].frames[boss[i].currentFrame],
    		boss[i].xCoord,
    		boss[i].yCoord,
    		boss[i].xCoord + boss[i].charWidth,
    		boss[i].yCoord + boss[i].charHeight );
    
    
    	boss[i].currentFrame++; //increments the "frame" by one, thus only animating one image at a time
    	if(boss[i].currentFrame == boss[i].animationFrames){
    		boss[i].currentFrame = 0;} //When frame 4 is reached, it resorts back to first frame and restarts
    		//character[i].drawable = 0;}
    	
    	boss[i].xCoord += boss[i].xMove;
    	boss[i].yCoord += boss[i].yMove;	
    	}
    Ok, so what this does is animates my boss for my game. Right now all he does is run rapidly around the screen. For the sake of time this is all I am going to have him do :P. Then whenever he crosses paths of character (charx = bosx or chary = bossy coordinates) the boss will shoot something at the character (yet to be implemented).

    But my issues are with using #pragma omp. My instructor tried showing this to our class in hopes it would help with our game, but I am still unable to have the boss going WHILE being able to control character. It can only do one or the other still.

    I guess I am not sure where the best placement of these #pragma's would be to achieve simultaneous movements.

  2. #2
    Registered User
    Join Date
    Mar 2013
    Posts
    31
    Why do you have the big if statement checking bossDirection and assigning newDirection if newDirection always = bossDirection?

    Also, why use pragma omp (not that I really have any idea what that is), why not just check for user input every iteration?

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    20
    Lol because that is what I used for my character movement control and it works so I just went with it :P. Saves me time hehe. Anyway, will checking for user input allow for both boss to be animated and move as well as character to be animated and move? Because that is what #pragma omp is supposed to allow for, but I am apparently not working it right.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    20
    Anyone at all offer #pragma help? From what I see debugging nothing I try is even making a new thread... it is all still tied into main thread. Should #pragma go in playBoss()? main() while loop that calls playBoss()? boss_animation()?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > bkimage = malloc(area);
    This is a lot of memory being allocated, with no obvious sign of when it is freed.

    > readimagefile(boss[i].frames[boss[i].currentFrame],
    This is expensive in an animation loop.
    Your data should be read once at startup, not every time while you're trying to animate.

    > Anyone at all offer #pragma help?
    Well you have to ask yourself where there is a lot of work being done in a short amount of time, that could possibly be split up.

    Line 38 makes no sense - yes it is a lot of work, but it contains a Sleep() call, presumably for the express intent of making sure it doesn't happen all at once!

    Consider something like this
    > getimage(0, 0, 799, 799, bkimage);

    You could do this in a parallel loop (with appropriate adjustments to bkimage pointer)
    getimage(0, 0, 200, 799, bkimage);
    getimage(200, 0, 200, 799, bkimage);
    getimage(400, 0, 200, 799, bkimage);
    getimage(600, 0, 200, 799, bkimage);

    You could do something similar when copying images to the screen as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    20
    Yeah I got rid of that malloc after posting. I realized that was causing my unhandled exceptions :P. And I got rid of completely putting in new background too. I am now replacing background by small sectors around the character to get rid of trail, instead of the entire background at once to up performance.

    And it is hard to look online to use pragma and parallel loop because yes there are examples, but they are always quite basic and nothing involving graphics in C... which makes it difficult to translate to where I would need to use it. It is difficult to find anything involving graphics in C :/.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #pragma
    By kantze in forum C Programming
    Replies: 2
    Last Post: 10-18-2006, 05:37 AM
  2. #pragma once
    By rodrigorules in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2005, 10:58 PM
  3. #pragma
    By pktcperlc++java in forum C Programming
    Replies: 2
    Last Post: 03-08-2005, 12:18 PM
  4. what does #pragma do?
    By cbc in forum C++ Programming
    Replies: 2
    Last Post: 10-16-2003, 06:51 AM
  5. #pragma ?
    By dat in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2003, 07:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21