Skip to content

Commit 2893fd0

Browse files
committed
converting from c to cpp
1 parent 633232e commit 2893fd0

File tree

7 files changed

+193
-23
lines changed

7 files changed

+193
-23
lines changed

Makefile

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
TARGET = BARNES
22
OBJS = code.o code_io.o load.o grav.o getparam.o util.o
33

4-
CC := gcc
4+
#CC := gcc
5+
CC := g++
56
#CC := clang
67
#CFLAGS := -O -pthread -D_POSIX_C_SOURCE=200112 -static -integrated-as -msoft-float
78
#CFLAGS := -O -pthread -D_POSIX_C_SOURCE=200112 -static -msoft-float
89
#CFLAGS := -O -pthread -D_POSIX_C_SOURCE=200112 -integrated-as -msoft-float
9-
CFLAGS := -O -pthread -D_POSIX_C_SOURCE=200112 -msoft-float
10+
#CFLAGS := -O -pthread -D_POSIX_C_SOURCE=200112 -msoft-float
11+
CFLAGS := -O -pthread -D_POSIX_C_SOURCE=200112
1012
#CFLAGS := -O3 -pthread -D_POSIX_C_SOURCE=200112
1113
#CFLAGS := -g3 -pthread -D_POSIX_C_SOURCE=200112
1214
CFLAGS := $(CFLAGS) -Wall -W -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wdisabled-optimization
@@ -23,7 +25,7 @@ $(TARGET): $(OBJS)
2325
$(CC) $(OBJS) $(CFLAGS) -o $(TARGET) $(LDFLAGS)
2426

2527
clean:
26-
rm -rf *.c *.h *.o $(TARGET)
28+
rm -rf *.c *.h *.o *.cpp $(TARGET)
2729

2830
.SUFFIXES:
2931
.SUFFIXES: .o .c .C .h .H
@@ -32,14 +34,14 @@ clean:
3234
$(M4) $(MACROS) $*.H > $*.h
3335

3436
.C.c:
35-
$(M4) $(MACROS) $*.C > $*.c
37+
$(M4) $(MACROS) $*.C > $*.cpp
3638

3739
.c.o:
38-
$(CC) -c $(CFLAGS) $*.c
40+
$(CC) -c $(CFLAGS) $*.cpp
3941

4042
.C.o:
41-
$(M4) $(MACROS) $*.C > $*.c
42-
$(CC) -c $(CFLAGS) $*.c
43+
$(M4) $(MACROS) $*.C > $*.cpp
44+
$(CC) -c $(CFLAGS) $*.cpp
4345

4446
stdinc.h: code.h defs.h util.h vectmath.h load.h code_io.h grav.h getparam.h stdinc.H
4547
code.o: code.C stdinc.h

code.C

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,34 @@ void stepsystem(long ProcessId)
637637

638638

639639
/* start at same time */
640-
BARRIER(Global->Barrier,NPROC);
640+
641+
{
642+
unsigned long Error, Cycle;
643+
long Cancel, Temp;
644+
645+
Error = pthread_mutex_lock(&(Global->Barrier).mutex);
646+
if (Error != 0) {
647+
printf("Error while trying to get lock in barrier.\n");
648+
exit(-1);
649+
}
650+
651+
Cycle = (Global->Barrier).cycle;
652+
if (++(Global->Barrier).counter != (NPROC)) {
653+
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, (int *) &Cancel);
654+
while (Cycle == (Global->Barrier).cycle) {
655+
Error = pthread_cond_wait(&(Global->Barrier).cv, &(Global->Barrier).mutex);
656+
if (Error != 0) {
657+
break;
658+
}
659+
}
660+
pthread_setcancelstate(Cancel, (int *) &Temp);
661+
} else {
662+
(Global->Barrier).cycle = !(Global->Barrier).cycle;
663+
(Global->Barrier).counter = 0;
664+
Error = pthread_cond_broadcast(&(Global->Barrier).cv);
665+
}
666+
pthread_mutex_unlock(&(Global->Barrier).mutex);
667+
}
641668

642669
if ((ProcessId == 0) && (Local[ProcessId].nstep >= 2)) {
643670
CLOCK(treebuildstart);
@@ -662,7 +689,7 @@ void stepsystem(long ProcessId)
662689
}
663690

664691
Local[ProcessId].mynbody = 0;
665-
find_my_bodies(Global->G_root, 0, BRC_FUC, ProcessId );
692+
find_my_bodies(reinterpret_cast<nodeptr>(Global->G_root), 0, BRC_FUC, ProcessId );
666693

667694
/* B*RRIER(Global->Barcom,NPROC); */
668695
if ((ProcessId == 0) && (Local[ProcessId].nstep >= 2)) {
@@ -714,7 +741,34 @@ void stepsystem(long ProcessId)
714741
/* bar needed to make sure that every process has computed its min */
715742
/* and max coordinates, and has accumulated them into the global */
716743
/* min and max, before the new dimensions are computed */
717-
BARRIER(Global->Barrier,NPROC);
744+
745+
{
746+
unsigned long Error, Cycle;
747+
long Cancel, Temp;
748+
749+
Error = pthread_mutex_lock(&(Global->Barrier).mutex);
750+
if (Error != 0) {
751+
printf("Error while trying to get lock in barrier.\n");
752+
exit(-1);
753+
}
754+
755+
Cycle = (Global->Barrier).cycle;
756+
if (++(Global->Barrier).counter != (NPROC)) {
757+
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, (int *) &Cancel);
758+
while (Cycle == (Global->Barrier).cycle) {
759+
Error = pthread_cond_wait(&(Global->Barrier).cv, &(Global->Barrier).mutex);
760+
if (Error != 0) {
761+
break;
762+
}
763+
}
764+
pthread_setcancelstate(Cancel, (int *) &Temp);
765+
} else {
766+
(Global->Barrier).cycle = !(Global->Barrier).cycle;
767+
(Global->Barrier).counter = 0;
768+
Error = pthread_cond_broadcast(&(Global->Barrier).cv);
769+
}
770+
pthread_mutex_unlock(&(Global->Barrier).mutex);
771+
}
718772

719773
if ((ProcessId == 0) && (Local[ProcessId].nstep >= 2)) {
720774
CLOCK(trackend);
@@ -786,7 +840,35 @@ void find_my_initial_bodies(bodyptr btab, long nbody, long ProcessId)
786840
for (i=0; i < Local[ProcessId].mynbody; i++) {
787841
Local[ProcessId].mybodytab[i] = &(btab[offset+i]);
788842
}
789-
BARRIER(Global->Barrier,NPROC);
843+
844+
{
845+
unsigned long Error, Cycle;
846+
long Cancel, Temp;
847+
848+
Error = pthread_mutex_lock(&(Global->Barrier).mutex);
849+
if (Error != 0) {
850+
printf("Error while trying to get lock in barrier.\n");
851+
exit(-1);
852+
}
853+
854+
Cycle = (Global->Barrier).cycle;
855+
if (++(Global->Barrier).counter != (NPROC)) {
856+
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, (int *) &Cancel);
857+
while (Cycle == (Global->Barrier).cycle) {
858+
Error = pthread_cond_wait(&(Global->Barrier).cv, &(Global->Barrier).mutex);
859+
if (Error != 0) {
860+
break;
861+
}
862+
}
863+
pthread_setcancelstate(Cancel, (int *) &Temp);
864+
} else {
865+
(Global->Barrier).cycle = !(Global->Barrier).cycle;
866+
(Global->Barrier).counter = 0;
867+
Error = pthread_cond_broadcast(&(Global->Barrier).cv);
868+
}
869+
pthread_mutex_unlock(&(Global->Barrier).mutex);
870+
}
871+
790872
}
791873

792874

code_io.C

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,33 @@ void output(long ProcessId)
127127
UNLOCK(Global->CountLock);
128128
}
129129

130-
BARRIER(Global->Barrier,NPROC);
130+
{
131+
unsigned long Error, Cycle;
132+
long Cancel, Temp;
133+
134+
Error = pthread_mutex_lock(&(Global->Barrier).mutex);
135+
if (Error != 0) {
136+
printf("Error while trying to get lock in barrier.\n");
137+
exit(-1);
138+
}
139+
140+
Cycle = (Global->Barrier).cycle;
141+
if (++(Global->Barrier).counter != (NPROC)) {
142+
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, (int *) &Cancel);
143+
while (Cycle == (Global->Barrier).cycle) {
144+
Error = pthread_cond_wait(&(Global->Barrier).cv, &(Global->Barrier).mutex);
145+
if (Error != 0) {
146+
break;
147+
}
148+
}
149+
pthread_setcancelstate(Cancel, (int *) &Temp);
150+
} else {
151+
(Global->Barrier).cycle = !(Global->Barrier).cycle;
152+
(Global->Barrier).counter = 0;
153+
Error = pthread_cond_broadcast(&(Global->Barrier).cv);
154+
}
155+
pthread_mutex_unlock(&(Global->Barrier).mutex);
156+
}
131157

132158
if (ProcessId==0) {
133159
nttot = Global->n2bcalc + Global->nbccalc;

getparam.C

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ EXTERN_ENV
2121
#define global extern
2222

2323
#include "stdinc.h"
24+
//#include <stdio.h>
2425

2526
local string *defaults = NULL; /* vector of "name=value" strings */
2627

@@ -49,10 +50,15 @@ string getparam(string name)
4950
if (i < 0)
5051
error("getparam: %s unknown\n", name);
5152
def = extrvalue(defaults[i]);
52-
gets(buf);
53+
fgets(buf, 128, stdin);
54+
size_t n = strlen(buf);
55+
56+
if (n > 0 && buf[n - 1] == '\n') {
57+
buf[n - 1] = '\0';
58+
}
5359
leng = strlen(buf) + 1;
5460
if (leng > 1) {
55-
return (strcpy(malloc(leng), buf));
61+
return (strcpy(static_cast<char*>(malloc(leng)), buf));
5662
}
5763
else {
5864
return (def);

grav.C

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void gravsub(register nodeptr p, long ProcessId)
9595

9696
void hackwalk(long ProcessId)
9797
{
98-
walksub(Global->G_root, Global->rsize * Global->rsize, ProcessId);
98+
walksub(reinterpret_cast <nodeptr>(Global->G_root), Global->rsize * Global->rsize, ProcessId);
9999
}
100100

101101
/*
@@ -122,7 +122,7 @@ void walksub(nodeptr n, real dsq, long ProcessId)
122122
for (i = 0; i < l->num_bodies; i++) {
123123
p = Bodyp(l)[i];
124124
if (p != Local[ProcessId].pskip) {
125-
gravsub(p, ProcessId);
125+
gravsub(reinterpret_cast <nodeptr>(p), ProcessId);
126126
}
127127
else {
128128
Local[ProcessId].skipself = TRUE;

load.C

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,63 @@ void maketree(long ProcessId)
4848
UNLOCK(Global->io_lock);
4949
}
5050
}
51-
BARRIER(Global->Barrier,NPROC);
51+
52+
{
53+
unsigned long Error, Cycle;
54+
long Cancel, Temp;
55+
56+
Error = pthread_mutex_lock(&(Global->Barrier).mutex);
57+
if (Error != 0) {
58+
printf("Error while trying to get lock in barrier.\n");
59+
exit(-1);
60+
}
61+
62+
Cycle = (Global->Barrier).cycle;
63+
if (++(Global->Barrier).counter != (NPROC)) {
64+
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, (int *) &Cancel);
65+
while (Cycle == (Global->Barrier).cycle) {
66+
Error = pthread_cond_wait(&(Global->Barrier).cv, &(Global->Barrier).mutex);
67+
if (Error != 0) {
68+
break;
69+
}
70+
}
71+
pthread_setcancelstate(Cancel, (int *) &Temp);
72+
} else {
73+
(Global->Barrier).cycle = !(Global->Barrier).cycle;
74+
(Global->Barrier).counter = 0;
75+
Error = pthread_cond_broadcast(&(Global->Barrier).cv);
76+
}
77+
pthread_mutex_unlock(&(Global->Barrier).mutex);
78+
}
79+
5280
hackcofm(ProcessId );
53-
BARRIER(Global->Barrier,NPROC);
81+
{
82+
unsigned long Error, Cycle;
83+
long Cancel, Temp;
84+
85+
Error = pthread_mutex_lock(&(Global->Barrier).mutex);
86+
if (Error != 0) {
87+
printf("Error while trying to get lock in barrier.\n");
88+
exit(-1);
89+
}
90+
91+
Cycle = (Global->Barrier).cycle;
92+
if (++(Global->Barrier).counter != (NPROC)) {
93+
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, (int *) &Cancel);
94+
while (Cycle == (Global->Barrier).cycle) {
95+
Error = pthread_cond_wait(&(Global->Barrier).cv, &(Global->Barrier).mutex);
96+
if (Error != 0) {
97+
break;
98+
}
99+
}
100+
pthread_setcancelstate(Cancel, (int *) &Temp);
101+
} else {
102+
(Global->Barrier).cycle = !(Global->Barrier).cycle;
103+
(Global->Barrier).counter = 0;
104+
Error = pthread_cond_broadcast(&(Global->Barrier).cv);
105+
}
106+
pthread_mutex_unlock(&(Global->Barrier).mutex);
107+
}
54108
}
55109

56110
cellptr InitCell(cellptr parent, long ProcessId)
@@ -157,7 +211,7 @@ void printtree(nodeptr n)
157211

158212
nodeptr loadtree(bodyptr p, cellptr root, long ProcessId)
159213
{
160-
long l, xp[NDIM], xor[NDIM], flag;
214+
long l, xp[NDIM], xor_arr[NDIM], flag;
161215
long i, j, root_level;
162216
bool valid_root;
163217
long kidIndex;
@@ -167,11 +221,11 @@ nodeptr loadtree(bodyptr p, cellptr root, long ProcessId)
167221
intcoord(xp, Pos(p));
168222
valid_root = TRUE;
169223
for (i = 0; i < NDIM; i++) {
170-
xor[i] = xp[i] ^ Local[ProcessId].Root_Coords[i];
224+
xor_arr[i] = xp[i] ^ Local[ProcessId].Root_Coords[i];
171225
}
172226
for (i = IMAX >> 1; i > Level(root); i >>= 1) {
173227
for (j = 0; j < NDIM; j++) {
174-
if (xor[j] & i) {
228+
if (xor_arr[j] & i) {
175229
valid_root = FALSE;
176230
break;
177231
}
@@ -189,7 +243,7 @@ nodeptr loadtree(bodyptr p, cellptr root, long ProcessId)
189243
valid_root = TRUE;
190244
for (i = IMAX >> 1; i > Level(root); i >>= 1) {
191245
for (j = 0; j < NDIM; j++) {
192-
if (xor[j] & i) {
246+
if (xor_arr[j] & i) {
193247
valid_root = FALSE;
194248
break;
195249
}

stdinc.H

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ typedef FILE *stream;
3939
* BOOL, TRUE and FALSE: standard names for logical values.
4040
*/
4141

42-
typedef long bool;
42+
//typedef long bool;
4343

4444
#ifndef TRUE
4545

0 commit comments

Comments
 (0)