| 1185 | } |
| 1186 | |
| 1187 | void ecall_log_grad(float *input,float *grad,int N,float *output){ |
| 1188 | float *x = (float*)malloc(sizeof(float)*N); |
| 1189 | memset(x, 0, sizeof(float)*N); |
| 1190 | for(int j=0;j<N;j++){ |
| 1191 | x[j] = 0.0f; |
| 1192 | float sum = input[insert*N+j]; |
| 1193 | float c = 0.0f; |
| 1194 | for(int i=0;i<Nt;i++){ |
| 1195 | float y = input[indexs[i]*N+j] - c; |
| 1196 | float t = sum + y; |
| 1197 | c = (t - sum) - y; |
| 1198 | sum = t; |
| 1199 | } |
| 1200 | x[j] = sum; |
| 1201 | |
| 1202 | } |
| 1203 | |
| 1204 | float *g = (float*)malloc(sizeof(float)*N); |
| 1205 | memset(g, 0, sizeof(float)*N); |
| 1206 | for(int j=0;j<N;j++){ |
| 1207 | g[j] = 0.0f; |
| 1208 | float sum = grad[insert*N+j]; |
| 1209 | float c = 0.0f; |
| 1210 | for(int i=0;i<Nt;i++){ |
| 1211 | float y = grad[indexs[i]*N+j] - c; |
| 1212 | float t = sum + y; |
| 1213 | c = (t - sum) - y; |
| 1214 | sum = t; |
| 1215 | } |
| 1216 | g[j] = sum; |
| 1217 | } |
| 1218 | |
| 1219 | for (int j = 0; j < N; j++) { |
| 1220 | float tmp = static_cast<float>(1)/(x[j]+10e-4f); |
| 1221 | g[j] = g[j]*tmp; |
| 1222 | } |
| 1223 | float mean = 0.0f, std, mean_sqr = 0.0f; |
| 1224 | float c_mean = 0.0f, c_mean_sqr = 0.0f; |
| 1225 | for (int j = 0; j < N; j++) { |
| 1226 | float y = (g[j] /static_cast<float>(N)) - c_mean; |
| 1227 | float t = mean + y; |
| 1228 | c_mean = (t - mean) - y; |
| 1229 | mean = t; |
| 1230 | |
| 1231 | float y2 = (g[j]*g[j]/static_cast<float>(N)) - c_mean_sqr; |
| 1232 | float t2 = mean_sqr + y2; |
| 1233 | c_mean_sqr = (t2 - mean_sqr) - y2; |
| 1234 | mean_sqr = t2; |
| 1235 | } |
| 1236 | std = sqrtf(fabs(mean_sqr - mean * mean))/static_cast<float>(Nt); |
| 1237 | mean = mean/static_cast<float>(Nt); |
| 1238 | for(int i=0;i<N*(Ne-1);i++){ |
| 1239 | output[i] =(g[i%N] - gaussrand(mean, std))/static_cast<float>(Nt+1); |
| 1240 | } |
| 1241 | for(int j=N*(Ne-1);j<N*Ne;j++){ |
| 1242 | output[j] = 0.0f; |
| 1243 | float sum = 0.0f; |
| 1244 | float c = 0.0f; |