Add support for variations.

--HG--
rename : cuburn/variations.py => cuburn/code/variations.py
This commit is contained in:
Steven Robertson
2011-05-01 09:36:29 -04:00
parent 088299423e
commit a7900f187d
6 changed files with 69 additions and 50 deletions

View File

@ -7,7 +7,7 @@ from pycuda.driver import In, Out, InOut
from pycuda.compiler import SourceModule
import numpy as np
from cuburn.code import mwc
from cuburn.code import mwc, variations
from cuburn.code.util import *
import tempita
@ -32,10 +32,18 @@ void apply_xf{{xfid}}(float *ix, float *iy, float *icolor,
float tx, ty, ox = *ix, oy = *iy;
{{apply_affine('ox', 'oy', 'tx', 'ty', px, 'xf.c', 'pre')}}
// tiny little TODO: variations
ox = 0;
oy = 0;
*ix = tx;
*iy = ty;
{{for v in xform.vars}}
if (1) {
float w = {{px.get('xf.var[%d]' % v)}};
{{variations.var_code[variations.var_nos[v]]()}}
}
{{endfor}}
*ix = ox;
*iy = oy;
float csp = {{px.get('xf.color_speed')}};
*icolor = *icolor * (1.0f - csp) + {{px.get('xf.color')}} * csp;
@ -64,7 +72,7 @@ void iter(mwc_st *msts, const iter_info *infos, float *accbuf, float *denbuf) {
float xfsel = mwc_next_01(&rctx);
{{for xfid, xform in enumerate(features.xforms)}}
if (xfsel < {{packer.get('cp.norm_density[%d]' % xfid)}}) {
if (xfsel <= {{packer.get('cp.norm_density[%d]' % xfid)}}) {
apply_xf{{xfid}}(&x, &y, &color, info);
} else
{{endfor}}
@ -78,9 +86,9 @@ void iter(mwc_st *msts, const iter_info *infos, float *accbuf, float *denbuf) {
continue;
}
if (x <= -1.0f || x >= 1.0f || y <= -1.0f || y >= 1.0f
|| consec_bad < 0) {
nsamps--;
if (x <= -1.0f || x >= 1.0f || y <= -1.0f || y >= 1.0f) {
consec_bad++;
if (consec_bad > {{features.max_oob}}) {
x = mwc_next_11(&rctx);
@ -92,8 +100,8 @@ void iter(mwc_st *msts, const iter_info *infos, float *accbuf, float *denbuf) {
}
// TODO: dither?
int i = ((int)((y + 1.0f) * 256.0f) * 512)
+ (int)((x + 1.0f) * 256.0f);
int i = ((int)((y + 1.0f) * 255.0f) * 512)
+ (int)((x + 1.0f) * 255.0f);
accbuf[i*4] += color < 0.5f ? (1.0f - 2.0f * color) : 0.0f;
accbuf[i*4+1] += 1.0f - 2.0f * fabsf(0.5f - color);
accbuf[i*4+2] += color > 0.5f ? color * 2.0f - 1.0f : 0.0f;
@ -101,7 +109,6 @@ void iter(mwc_st *msts, const iter_info *infos, float *accbuf, float *denbuf) {
denbuf[i] += 1.0f;
nsamps--;
}
}
""")
@ -118,7 +125,7 @@ def silly(features, cp):
iter = IterCode(features)
code = assemble_code(BaseCode, mwc.MWC, iter, iter.packer)
print code
mod = SourceModule(code)
mod = SourceModule(code, options=['-use_fast_math'], keep=True)
info = iter.packer.pack(cp=cp)
print info

View File

@ -3,7 +3,6 @@ The multiply-with-carry random number generator.
"""
import numpy as np
import tempita
from cuburn.code.util import *

40
cuburn/code/variations.py Normal file
View File

@ -0,0 +1,40 @@
var_nos = {}
var_code = {}
def _var(num, name = None):
def _var1(func):
if name:
namelo = name
else:
namelo = func.__name__
var_nos[num] = namelo
var_code[namelo] = func
return _var1
# Variables note: all functions will have their weights as 'w',
# input variables 'tx' and 'ty', and output 'ox' and 'oy' available
# from the calling context. Each statement will be placed inside brackets,
# to avoid namespace pollution.
@_var(0)
def linear():
return """
ox += tx * w;
oy += ty * w;
"""
@_var(1)
def sinusoidal():
return """
ox += w * sin(tx);
oy += w * sin(ty);
"""
@_var(2)
def spherical():
return """
float r2 = w / (tx*tx + ty*ty);
ox += tx * r2;
oy += ty * r2;
"""