Better protect BA81 from parallel execution
[openmx:openmx.git] / src / omxExpectation.h
1 /*
2  *  Copyright 2007-2014 The OpenMx Project
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *   Unless required by applicable law or agreed to in writing, software
11  *   distributed under the License is distributed on an "AS IS" BASIS,
12  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 /***********************************************************
18
19 *  omxExpectation.h
20 *
21 *  Created: Timothy R. Brick    Date: 2009-02-17
22 *
23 *       Contains header information for the omxExpectation class
24 *   omxExpectation objects hold necessary information to simplify
25 *       Expectation function calculation.
26 *
27 **********************************************************/
28
29 #ifndef _OMXEXPECTATION_H_
30 #define _OMXEXPECTATION_H_
31
32 #define R_NO_REMAP
33 #include <R.h>
34 #include <Rinternals.h> 
35 #include <R_ext/Rdynload.h> 
36 #include <R_ext/BLAS.h>
37 #include <R_ext/Lapack.h>
38
39 #include "omxDefines.h"
40 #include "omxMatrix.h"
41 #include "omxAlgebra.h"
42 #include "omxAlgebraFunctions.h"
43 #include "omxData.h"
44 #include "omxState.h"
45
46 /* Def Var and Threshold Structures */
47 struct omxDefinitionVar {                       // Definition Var
48
49         int data, column;               // Where it comes from
50         omxData* source;                // Data source
51         int numLocations;               // Num locations
52         int* rows;                              // row positions
53         int* cols;                              // column positions
54         int* matrices;                  // matrix numbers
55         int  numDeps;           // number of algebra/matrix dependencies
56         int* deps;              // indices of algebra/matrix dependencies
57
58 };
59
60 /* Expectation structure itself */
61 struct omxExpectation {                                 // An Expectation
62
63         /* Fields unique to Expectation Functions */
64         void (*initFun)(omxExpectation *ox);
65         void (*destructFun)(omxExpectation* ox);                                                                        // Wrapper for the destructor object
66         void (*computeFun)(omxExpectation* ox, const char *what, const char *how);
67         void (*printFun)(omxExpectation* ox);                                                                           // Prints the appropriate pieces of the expectation
68         void (*populateAttrFun)(omxExpectation* ox, SEXP algebra);                                      // Add attributes to the result algebra object
69         void (*setVarGroup)(omxExpectation*, FreeVarGroup *);  // TODO remove
70         
71         // componentfun & mutateFun probably take encapsulation a little too seriously.
72         // The Fit function should probably just include the structure definition
73         // for the expectation and access fields directly or through object methods.
74         omxMatrix* (*componentFun)(omxExpectation*, omxFitFunction*, const char*);
75         void (*mutateFun)(omxExpectation*, omxFitFunction*, const char*, omxMatrix*);
76
77         SEXP rObj;                                                                                                                                      // Original r Object Pointer
78         void* argStruct;                                                                                                                        // Arguments needed for Expectation function
79         const char* expType;   // pointer to a static string, no need to allocate or free
80
81         omxData* data;                                                                                                                          // Not sure if this is appropriate, but the expectation passes the actual data object
82         omxMatrix* dataColumns;
83         omxThresholdColumn* thresholds;
84         omxDefinitionVar* defVars;
85         int numOrdinal, numDefs;
86         
87         /* Replication of some of the structures from Matrix */
88         unsigned short isComplete;                                                                                                      // Whether or not this expectation has been initialize
89         omxState* currentState;
90         int expNum;
91
92         omxExpectation *container;
93         int numSubmodels;
94         omxExpectation **submodels;
95
96         // omxExpectation should not need to know about free variables.
97         FreeVarGroup *freeVarGroup; // TODO remove
98         const char *name;
99
100         bool canDuplicate;
101 };
102
103 omxExpectation *
104 omxNewInternalExpectation(const char *expType, omxState* os);
105
106         void omxCompleteExpectation(omxExpectation *ox);
107 void setFreeVarGroup(omxExpectation *ox, FreeVarGroup *fvg);
108
109         void omxFreeExpectationArgs(omxExpectation* Expectation);                                       // Frees all args
110 omxExpectation* omxExpectationFromIndex(int expIndex, omxState* os);
111         omxExpectation* omxNewIncompleteExpectation(SEXP mxobj, int expNum, omxState* os);
112         
113
114 /* Expectation-specific implementations of matrix functions */
115 void omxExpectationRecompute(omxExpectation *ox);
116 void omxExpectationCompute(omxExpectation *ox, const char *what, const char *how);
117
118 static inline void omxExpectationCompute(omxExpectation *ox, const char *what)
119 { omxExpectationCompute(ox, what, NULL); }
120
121         omxExpectation* omxDuplicateExpectation(const omxExpectation *src, omxState* newState);
122         
123         void omxExpectationPrint(omxExpectation *source, char* d);                                      // Pretty-print a (small-form) expectation
124         
125 omxMatrix* omxGetExpectationComponent(omxExpectation *ox, omxFitFunction *off, const char* component);
126         
127 void omxSetExpectationComponent(omxExpectation *ox, omxFitFunction *off, const char* component, omxMatrix *om);
128
129 void omxInitNormalExpectation(omxExpectation *ox);
130 void omxInitLISRELExpectation(omxExpectation *ox);
131 void omxInitStateSpaceExpectation(omxExpectation *ox);
132 void omxInitRAMExpectation(omxExpectation *ox);
133 void omxInitExpectationBA81(omxExpectation* oo);
134
135 #endif /* _OMXEXPECTATION_H_ */