Actual source code: ex2.c
1: static char help[] = "Tests PetscRandom functions.\n\n";
3: #include <stdlib.h>
4: #include <stdio.h>
5: #include <string.h>
6: #include <time.h>
7: #include <sys/types.h>
9: #include petscsys.h
11: #define MAXBSIZE 40
12: #define PI 3.1415926535897
13: #define DATAFILENAME "ex2_stock.txt"
15: struct himaInfoTag {
16: int n;
17: double r;
18: double dt;
19: int totalNumSim;
20: double *St0;
21: double *vol;
22: };
23: typedef struct himaInfoTag himaInfo;
25: /* function protype */
26: PetscErrorCode readData(MPI_Comm comm,himaInfo *hinfo);
27: double mcVal(double St, double r, double vol, double dt, double eps);
28: void exchange(double *a, double *b);
29: double basketPayoff(double vol[], double St0[], int n, double r,double dt, double eps[]);
30: void stdNormalArray(double *eps, unsigned long size,PetscRandom ran);
31: unsigned long divWork(int id, unsigned long num, int np);
33: /*
34: Contributed by Xiaoyan Zeng <zengxia@iit.edu> and Liu, Kwong Ip" <kiliu@math.hkbu.edu.hk>
36: Example of usage:
37: mpiexec -n 4 ./ex2 -num_of_stocks 30 -interest_rate 0.4 -time_interval 0.01 -num_of_simulations 10000
38: */
42: int main(int argc, char *argv[])
43: {
44: double r,dt;
45: int n;
46: unsigned long i,myNumSim,totalNumSim,numdim;
47: double payoff;
48: double *vol, *St0, x, totalx;
49: int np,myid;
50: time_t start,stop;
51: double *eps;
52: himaInfo hinfo;
53: PetscRandom ran;
55: PetscTruth flg;
57: PetscInitialize(&argc,&argv,(char *)0,help);
58: time(&start);
59: PetscRandomCreate(PETSC_COMM_WORLD,&ran);
60: #if defined(PETSC_HAVE_SPRNG)
61: PetscRandomSetType(ran,PETSCSPRNG);
62: #elif defined(PETSC_HAVE_RAND)
63: PetscRandomSetType(ran,PETSCRAND);
64: #endif
65: PetscRandomSetFromOptions(ran);
68: MPI_Comm_size(PETSC_COMM_WORLD, &np); /* number of nodes */
69: MPI_Comm_rank(PETSC_COMM_WORLD, &myid); /* my ranking */
71: PetscOptionsHasName(PETSC_NULL, "-check_generators", &flg);
72: if (flg){
73: PetscRandomGetValue(ran,&r);
74: PetscSynchronizedPrintf(PETSC_COMM_WORLD,"[%d] rval: %g\n",myid,r);
75: PetscSynchronizedFlush(PETSC_COMM_WORLD);
76: }
77:
78: hinfo.n = 31;
79: hinfo.r = 0.04;
80: hinfo.dt = 1.0/12; /* a month as a period */
81: hinfo.totalNumSim = 1000;
82: PetscOptionsGetInt(PETSC_NULL,"-num_of_stocks",&(hinfo.n),PETSC_NULL);
83: if (hinfo.n <1 || hinfo.n > 31) SETERRQ1(PETSC_ERR_SUP,"Only 31 stocks listed in stock.txt. num_of_stocks %D must between 1 and 31",hinfo.n);
84: PetscOptionsGetReal(PETSC_NULL,"-interest_rate",&(hinfo.r),PETSC_NULL);
85: PetscOptionsGetReal(PETSC_NULL,"-time_interval",&(hinfo.dt),PETSC_NULL);
86: PetscOptionsGetInt(PETSC_NULL,"-num_of_simulations",&(hinfo.totalNumSim),PETSC_NULL);
88: n = hinfo.n;
89: r = hinfo.r;
90: dt = hinfo.dt;
91: totalNumSim = hinfo.totalNumSim;
92: vol = hinfo.vol = (double *)malloc(sizeof(double)*(2*n+1));
93: St0 = hinfo.St0 = hinfo.vol + n;
94: readData(PETSC_COMM_WORLD,&hinfo);
96: numdim = n*(n+1)/2;
97: if (numdim%2 == 1){
98: numdim++;
99: }
100: eps = (double *)malloc(sizeof(double)*numdim);
102: myNumSim = divWork(myid,totalNumSim,np);
104: x = 0;
105: for (i=0;i<myNumSim;i++){
106: stdNormalArray(eps,numdim,ran);
107: x += basketPayoff(vol,St0,n,r,dt,eps);
108: }
110: MPI_Reduce(&x, &totalx, 1, MPI_DOUBLE, MPI_SUM,0,PETSC_COMM_WORLD);
111: time(&stop);
112: if (myid == 0){
113: payoff = exp(-r*dt*n)*(totalx/totalNumSim);
114: PetscPrintf(PETSC_COMM_SELF,"Option price = $%.3f "
115: "using %ds of %s computation with %d %s "
116: "for %d stocks, %d trading period per year, "
117: "%.2f%% interest rate\n",
118: payoff,(int)(stop - start),"parallel",np,"processors",n,
119: (int)(1/dt),r);
120: }
121:
122: free(vol);
123: free(eps);
124: PetscRandomDestroy(ran);
125: PetscFinalize();
126: return 0;
127: }
129: void stdNormalArray(double *eps, unsigned long size, PetscRandom ran)
130: {
131: int i;
132: double u1,u2,t;
135: for (i=0;i<size;i+=2){
136: PetscRandomGetValue(ran,&u1);
137: PetscRandomGetValue(ran,&u2);
138:
139: t = sqrt(-2*log(u1));
140: eps[i] = t * cos(2*PI*u2);
141: eps[i+1] = t * sin(2*PI*u2);
142: }
143: }
146: double basketPayoff(double vol[], double St0[], int n, double r,double dt, double eps[])
147: {
148: double Stk[MAXBSIZE], temp;
149: double payoff;
150: int maxk,i,j;
151: int pointcount=0;
152:
153: for (i=0;i<n;i++) {
154: Stk[i] = St0[i];
155: }
157: for (i=0;i<n;i++){
158: maxk = 0;
159: for (j=0;j<(n-i);j++){
160: Stk[j] = mcVal(Stk[j],r,vol[j],dt,eps[pointcount++]);
161: if ((Stk[j]/St0[j]) > (Stk[maxk]/St0[maxk])){
162: maxk = j;
163: }
164: }
165: exchange(Stk+j-1,Stk+maxk);
166: exchange(St0+j-1,St0+maxk);
167: exchange(vol+j-1,vol+maxk);
168: }
169:
170: payoff = 0;
171: for (i=0;i<n;i++){
172: temp = (Stk[i]/St0[i]) - 1 ;
173: if (temp > 0) payoff += temp;
174: }
175: return payoff;
176: }
180: PetscErrorCode readData(MPI_Comm comm,himaInfo *hinfo)
181: {
182: int i;
183: FILE *fd;
184: char temp[50];
186: PetscMPIInt rank;
187: double *v = hinfo->vol, *t = hinfo->St0;
188: int num=hinfo->n;
189:
191: MPI_Comm_rank(comm,&rank);
192: if (!rank){
193: PetscFOpen(PETSC_COMM_SELF,DATAFILENAME,"r",&fd);
194: for (i=0;i<num;i++){
195: fscanf(fd,"%s%lf%lf",temp,v+i,t+i);
196: }
197: fclose(fd);
198: }
199: MPI_Bcast(v,2*num,MPI_DOUBLE,0,PETSC_COMM_WORLD);
200: //PetscPrintf(PETSC_COMM_SELF,"[%d] vol %g, ... %g; St0 %g, ... %g\n",rank,hinfo->vol[0],hinfo->vol[num-1],hinfo->St0 [0],hinfo->St0[num-1]);
201: return(0);
202: }
204: void exchange(double *a, double *b)
205: {
206: double t;
207:
208: t = *a;
209: *a = *b;
210: *b = t;
211: }
213: double mcVal(double St, double r, double vol, double dt, double eps)
214: {
215: return (St * exp((r-0.5*vol*vol)*dt + vol*sqrt(dt)*eps));
216: }
218: unsigned long divWork(int id, unsigned long num, int np)
219: {
220: unsigned long numit;
222: numit = (unsigned long)(((double)num)/np);
223: numit++;
224: return numit;
225: }