Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
tsoc
openmm
Commits
a6fe70d2
Commit
a6fe70d2
authored
Sep 16, 2014
by
peastman
Browse files
JIT compilation uses optimized calculations for many of the most common operations
parent
a83607d6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
107 additions
and
17 deletions
+107
-17
libraries/lepton/include/lepton/CompiledExpression.h
libraries/lepton/include/lepton/CompiledExpression.h
+1
-0
libraries/lepton/src/CompiledExpression.cpp
libraries/lepton/src/CompiledExpression.cpp
+106
-17
No files found.
libraries/lepton/include/lepton/CompiledExpression.h
View file @
a6fe70d2
...
...
@@ -89,6 +89,7 @@ private:
mutable
std
::
vector
<
double
>
workspace
;
mutable
std
::
vector
<
double
>
argValues
;
std
::
map
<
std
::
string
,
double
>
dummyVariables
;
std
::
vector
<
double
>
constants
;
asmjit
::
JitRuntime
runtime
;
void
*
jitCode
;
};
...
...
libraries/lepton/src/CompiledExpression.cpp
View file @
a6fe70d2
...
...
@@ -172,37 +172,126 @@ void CompiledExpression::generateJitCode() {
c
.
mov
(
workspacePointer
,
imm_ptr
(
&
workspace
[
0
]));
c
.
mov
(
argsPointer
,
imm_ptr
(
&
argValues
[
0
]));
// Load the variables.
// Load the
arguments into
variables.
for
(
set
<
string
>::
const_iterator
iter
=
variableNames
.
begin
();
iter
!=
variableNames
.
end
();
++
iter
)
{
map
<
string
,
int
>::
iterator
index
=
variableIndices
.
find
(
*
iter
);
c
.
movsd
(
workspaceVar
[
index
->
second
],
x86
::
ptr
(
workspacePointer
,
8
*
index
->
second
,
0
));
}
// Make a list of all constants that will be needed for evaluation.
vector
<
int
>
operationConstantIndex
(
operation
.
size
(),
-
1
);
for
(
int
step
=
0
;
step
<
(
int
)
operation
.
size
();
step
++
)
{
// Find the constant value (if any) used by this operation.
Operation
&
op
=
*
operation
[
step
];
double
value
;
if
(
op
.
getId
()
==
Operation
::
CONSTANT
)
value
=
dynamic_cast
<
Operation
::
Constant
&>
(
op
).
getValue
();
else
if
(
op
.
getId
()
==
Operation
::
ADD_CONSTANT
)
value
=
dynamic_cast
<
Operation
::
AddConstant
&>
(
op
).
getValue
();
else
if
(
op
.
getId
()
==
Operation
::
MULTIPLY_CONSTANT
)
value
=
dynamic_cast
<
Operation
::
MultiplyConstant
&>
(
op
).
getValue
();
else
if
(
op
.
getId
()
==
Operation
::
RECIPROCAL
)
value
=
1.0
;
else
continue
;
// See if we already have a variable for this constant.
for
(
int
i
=
0
;
i
<
(
int
)
constants
.
size
();
i
++
)
if
(
value
==
constants
[
i
])
{
operationConstantIndex
[
step
]
=
i
;
break
;
}
if
(
operationConstantIndex
[
step
]
==
-
1
)
{
operationConstantIndex
[
step
]
=
constants
.
size
();
constants
.
push_back
(
value
);
}
}
// Load constants into variables.
vector
<
X86XmmVar
>
constantVar
(
constants
.
size
());
X86GpVar
constantsPointer
(
c
);
c
.
mov
(
constantsPointer
,
imm_ptr
(
&
constants
[
0
]));
for
(
int
i
=
0
;
i
<
(
constants
.
size
());
i
++
)
{
constantVar
[
i
]
=
c
.
newXmmVar
(
kX86VarTypeXmmSd
);
c
.
movsd
(
constantVar
[
i
],
x86
::
ptr
(
constantsPointer
,
8
*
i
,
0
));
}
// Evaluate the operations.
for
(
int
step
=
0
;
step
<
(
int
)
operation
.
size
();
step
++
)
{
const
vector
<
int
>&
args
=
arguments
[
step
];
Operation
&
op
=
*
operation
[
step
];
vector
<
int
>
args
=
arguments
[
step
];
if
(
args
.
size
()
==
1
)
{
// One or more sequential arguments.
// One or more sequential arguments.
Fill out the list.
for
(
int
i
=
0
;
i
<
op
eration
[
step
]
->
getNumArguments
();
i
++
)
c
.
movsd
(
x86
::
ptr
(
argsPointer
,
8
*
i
,
0
),
workspaceVar
[
args
[
0
]
+
i
]
);
for
(
int
i
=
1
;
i
<
op
.
getNumArguments
();
i
++
)
args
.
push_back
(
args
[
0
]
+
i
);
}
else
{
// Two or more non-sequential arguments.
for
(
int
i
=
0
;
i
<
(
int
)
args
.
size
();
i
++
)
c
.
movsd
(
x86
::
ptr
(
argsPointer
,
8
*
i
,
0
),
workspaceVar
[
args
[
i
]]);
switch
(
op
.
getId
())
{
case
Operation
::
CONSTANT
:
c
.
movsd
(
workspaceVar
[
target
[
step
]],
constantVar
[
operationConstantIndex
[
step
]]);
break
;
case
Operation
::
ADD
:
c
.
movsd
(
workspaceVar
[
target
[
step
]],
workspaceVar
[
args
[
0
]]);
c
.
addsd
(
workspaceVar
[
target
[
step
]],
workspaceVar
[
args
[
1
]]);
break
;
case
Operation
::
SUBTRACT
:
c
.
movsd
(
workspaceVar
[
target
[
step
]],
workspaceVar
[
args
[
0
]]);
c
.
subsd
(
workspaceVar
[
target
[
step
]],
workspaceVar
[
args
[
1
]]);
break
;
case
Operation
::
MULTIPLY
:
c
.
movsd
(
workspaceVar
[
target
[
step
]],
workspaceVar
[
args
[
0
]]);
c
.
mulsd
(
workspaceVar
[
target
[
step
]],
workspaceVar
[
args
[
1
]]);
break
;
case
Operation
::
DIVIDE
:
c
.
movsd
(
workspaceVar
[
target
[
step
]],
workspaceVar
[
args
[
0
]]);
c
.
divsd
(
workspaceVar
[
target
[
step
]],
workspaceVar
[
args
[
1
]]);
break
;
case
Operation
::
NEGATE
:
c
.
xorps
(
workspaceVar
[
target
[
step
]],
workspaceVar
[
target
[
step
]]);
c
.
subsd
(
workspaceVar
[
target
[
step
]],
workspaceVar
[
args
[
0
]]);
break
;
case
Operation
::
SQRT
:
c
.
sqrtsd
(
workspaceVar
[
target
[
step
]],
workspaceVar
[
args
[
0
]]);
break
;
case
Operation
::
SQUARE
:
c
.
movsd
(
workspaceVar
[
target
[
step
]],
workspaceVar
[
args
[
0
]]);
c
.
mulsd
(
workspaceVar
[
target
[
step
]],
workspaceVar
[
args
[
0
]]);
break
;
case
Operation
::
CUBE
:
c
.
movsd
(
workspaceVar
[
target
[
step
]],
workspaceVar
[
args
[
0
]]);
c
.
mulsd
(
workspaceVar
[
target
[
step
]],
workspaceVar
[
args
[
0
]]);
c
.
mulsd
(
workspaceVar
[
target
[
step
]],
workspaceVar
[
args
[
0
]]);
break
;
case
Operation
::
RECIPROCAL
:
c
.
movsd
(
workspaceVar
[
target
[
step
]],
constantVar
[
operationConstantIndex
[
step
]]);
c
.
divsd
(
workspaceVar
[
target
[
step
]],
workspaceVar
[
args
[
0
]]);
break
;
case
Operation
::
ADD_CONSTANT
:
c
.
movsd
(
workspaceVar
[
target
[
step
]],
workspaceVar
[
args
[
0
]]);
c
.
addsd
(
workspaceVar
[
target
[
step
]],
constantVar
[
operationConstantIndex
[
step
]]);
break
;
case
Operation
::
MULTIPLY_CONSTANT
:
c
.
movsd
(
workspaceVar
[
target
[
step
]],
workspaceVar
[
args
[
0
]]);
c
.
mulsd
(
workspaceVar
[
target
[
step
]],
constantVar
[
operationConstantIndex
[
step
]]);
break
;
default:
for
(
int
i
=
0
;
i
<
(
int
)
args
.
size
();
i
++
)
c
.
movsd
(
x86
::
ptr
(
argsPointer
,
8
*
i
,
0
),
workspaceVar
[
args
[
i
]]);
X86GpVar
fn
(
c
,
kVarTypeIntPtr
);
c
.
mov
(
fn
,
imm_ptr
((
void
*
)
evaluateOperation
));
X86CallNode
*
call
=
c
.
call
(
fn
,
kFuncConvHost
,
FuncBuilder2
<
double
,
Operation
*
,
double
*>
());
call
->
setArg
(
0
,
imm_ptr
(
&
op
));
call
->
setArg
(
1
,
imm_ptr
(
&
argValues
[
0
]));
call
->
setRet
(
0
,
workspaceVar
[
target
[
step
]]);
}
X86GpVar
fn
(
c
,
kVarTypeIntPtr
);
c
.
mov
(
fn
,
imm_ptr
((
void
*
)
evaluateOperation
));
X86CallNode
*
call
=
c
.
call
(
fn
,
kFuncConvHost
,
FuncBuilder2
<
double
,
Operation
*
,
double
*>
());
call
->
setArg
(
0
,
imm_ptr
(
operation
[
step
]));
call
->
setArg
(
1
,
imm_ptr
(
&
argValues
[
0
]));
call
->
setRet
(
0
,
workspaceVar
[
target
[
step
]]);
}
c
.
ret
(
workspace
Pointer
);
c
.
ret
(
workspace
Var
[
workspace
.
size
()
-
1
]
);
c
.
endFunc
();
jitCode
=
c
.
make
();
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment