Hi,
I am posting the interesting part of my code, but I am not sure it will be so easy to read. The code mix fortran and C.
First, the main code is in C, but needs to call fortran procedure. Everything is standard and works fine until it reaches this part of the fortran code:
Code:
double precision function dWeffdcostree(sqrtS,costh)
implicit none
#include "decl.h"
#include "Weff_tree.h"
double precision sqrtS,costh
double precision result
double precision result_tmp(NCOMP)
result=0.D0
if((sqrtS .ge. (aMNeu(1)+aMNeu(1))) .AND.
& ((aMNeu(1)+aMNeu(1)) .le. maxenergy)) then
call dlwefftree("n1n1",result_tmp,sqrtS,costh);
(...)
dlwefftree needs the following routine in C, which dlopen/create librelictreedyn.so:
Code:
void dlwefftree_(char* name[], double result[], double* sqrtS, double* costh)
{
char Weffname[100];
void* handle=dlopen("src/relic/sharedlibtree/librelictreedyn.so", RTLD_LAZY);
sprintf(Weffname,"weff_tree_%s_",name);
void (*Weff)(double result[], double* sqrtS, double* costh) = dlsym(handle,Weffname);
char *error;
if( (error=dlerror()) != NULL )
{
if(handle) dlclose(handle);
/* printf("%s\n",error); */
char complete_name[300];
chdir("src/relic/sharedlibtree");
sprintf(complete_name,"rm -f *.o");
system(complete_name);
sprintf(complete_name,"%s %s -c ../Weff_tree/Weff_tree_%s.F -I../Weff_tree -I..",FC,FFLAGS,name);
printf("%s\n",complete_name);
system(complete_name);
sprintf(complete_name,"ar x librelictreedyn.a");
system(complete_name);
sprintf(complete_name,"ld -shared -o librelictreedyn.so *.o");
system(complete_name);
sprintf(complete_name,"ar r librelictreedyn.a *.o");
system(complete_name);
sprintf(complete_name,"rm -f *.o");
system(complete_name);
chdir("../../..");
handle=dlopen("./src/relic/sharedlibtree/librelictreedyn.so", RTLD_LAZY);
/* if( !handle ) printf("%s\n",dlerror()); */
void (*Weff)(double result[], double* sqrtS, double* costh) = dlsym(handle,Weffname);
/* if( (error=dlerror()) != NULL ) printf("%s\n",error); */
}
(*Weff)(result,sqrtS,costh);
dlclose(handle);
return;
}
The routine which should be in (*Weff) should be the following (in fortran):
Code:
subroutine weff_tree_n1n1(result,sqrtS,costh)
implicit none
#include "decl.h"
#include "Weff_tree.h"
double precision sqrtS,costh
double precision result(NCOMP)
double precision result_tmp(NCOMP)
call Weff_tree_n1n1ZZ(result_tmp,sqrtS,costh)
end
Weff_tree_n1n1ZZ is written as:
Code:
subroutine Weff_tree_n1n1ZZ(result,sqrtS,costh)
implicit none
#include "decl.h"
double precision result(NCOMP),result_tmp(NCOMP)
double precision sqrtS
double precision costh
call squaredtree("n1n1ZZ",result_tmp, helicities, flags)
result(1)=result_tmp(1)
end
squaredtree is a C function dlopening or creating the library libsquaredtreedyn.so:
Code:
void squaredtree_(char* name[], double result[], double* sqrtS, double* costh)
{
char Weffname[100];
int i=0;
void* handle=dlopen("src/relic/sharedlibtree/libsquaredtreedyn.so", RTLD_LAZY);
sprintf(Weffname,"tree_%ssquaredme_",name);
while (Weffname[i])
{
Weffname[i]=tolower(Weffname[i]);
i++;
}
void (*Weff)(double result[], double* sqrtS, double* costh) = dlsym(handle,Weffname);
char *error;
if( (error=dlerror()) != NULL )
{
if(handle) dlclose(handle);
char complete_name[300];
chdir("src/relic/sharedlibtree");
sprintf(complete_name,"rm -f *.o");
system(complete_name);
sprintf(complete_name,"%s %s -c ../squared_tree/squaredmetree_%s/squaredtree_%s.F -I../squared_tree/squaredmetree_%s -I..",FC,FFLAGS,name,name,name);
printf("%s\n",complete_name);
system(complete_name);
sprintf(complete_name,"ar x libsquaredtreedyn.a");
system(complete_name);
sprintf(complete_name,"ld -shared -o libsquaredtreedyn.so *.o");
system(complete_name);
sprintf(complete_name,"ar r libsquaredtreedyn.a *.o");
system(complete_name);
sprintf(complete_name,"rm -f *.o");
system(complete_name);
handle=dlopen("./libsquaredtreedyn.so", RTLD_LAZY);
if( !handle ) printf("%s\n",dlerror());
void (*Weff)(double result[], double* sqrtS, double* costh) = dlsym(handle,Weffname);
chdir("../../..");
if( (error=dlerror()) != NULL ) printf("%s\n",error);
}
(*Weff)(result,sqrtS,costh);
dlclose(handle);
return;
}
At the end, the routine in (*Weff) is a big fortran code which was working pretty well when static...
Hope this can help...
Note that I simplified a bit the code. I hope I have not broken anything.
Thanks,
CCox