Add internal utility for zeroing int vectors.

Remove unnecessary branches in related functions.

PiperOrigin-RevId: 562768873
Change-Id: I3964a7a953d95bcd4e29137a7b590be5ffa6e311
This commit is contained in:
Yuval Tassa
2023-09-05 07:13:37 -07:00
committed by Copybara-Service
parent f976d6d228
commit d5292976de
10 changed files with 30 additions and 31 deletions
+5 -6
View File
@@ -16,7 +16,6 @@
#include <stdio.h> #include <stdio.h>
#include <stddef.h> #include <stddef.h>
#include <string.h>
#include <mujoco/mjdata.h> #include <mujoco/mjdata.h>
#include <mujoco/mjmacro.h> #include <mujoco/mjmacro.h>
@@ -252,7 +251,7 @@ int mj_addConstraint(const mjModel* m, mjData* d,
// copy if not empty // copy if not empty
if (NV) { if (NV) {
memcpy(ind + adr[nefc+i], chain, sizeof(int)*NV); mju_copyInt(ind + adr[nefc+i], chain, NV);
mju_copy(J + adr[nefc+i], jac + i*NV, NV); mju_copy(J + adr[nefc+i], jac + i*NV, NV);
} }
} }
@@ -644,11 +643,11 @@ void mj_instantiateEquality(const mjModel* m, mjData* d) {
// add first or second chain // add first or second chain
if (j == 0) { if (j == 0) {
NV = d->ten_J_rownnz[id[j]]; NV = d->ten_J_rownnz[id[j]];
memcpy(chain, d->ten_J_colind+d->ten_J_rowadr[id[j]], NV*sizeof(int)); mju_copyInt(chain, d->ten_J_colind+d->ten_J_rowadr[id[j]], NV);
mju_copy(jac[j], d->ten_J+d->ten_J_rowadr[id[j]], NV); mju_copy(jac[j], d->ten_J+d->ten_J_rowadr[id[j]], NV);
} else { } else {
NV2 = d->ten_J_rownnz[id[j]]; NV2 = d->ten_J_rownnz[id[j]];
memcpy(chain2, d->ten_J_colind+d->ten_J_rowadr[id[j]], NV2*sizeof(int)); mju_copyInt(chain2, d->ten_J_colind+d->ten_J_rowadr[id[j]], NV2);
mju_copy(jac[j], d->ten_J+d->ten_J_rowadr[id[j]], NV2); mju_copy(jac[j], d->ten_J+d->ten_J_rowadr[id[j]], NV2);
} }
} else { } else {
@@ -1530,10 +1529,10 @@ static inline int mj_ne(const mjModel* m, mjData* d, int* nnz) {
} else { } else {
if (!j) { if (!j) {
NV = d->ten_J_rownnz[id[j]]; NV = d->ten_J_rownnz[id[j]];
memcpy(chain, d->ten_J_colind+d->ten_J_rowadr[id[j]], NV*sizeof(int)); mju_copyInt(chain, d->ten_J_colind+d->ten_J_rowadr[id[j]], NV);
} else { } else {
NV2 = d->ten_J_rownnz[id[j]]; NV2 = d->ten_J_rownnz[id[j]];
memcpy(chain2, d->ten_J_colind+d->ten_J_rowadr[id[j]], NV2*sizeof(int)); mju_copyInt(chain2, d->ten_J_colind+d->ten_J_rowadr[id[j]], NV2);
} }
} }
} }
+1 -1
View File
@@ -874,7 +874,7 @@ static void makeDSparse(const mjModel* m, mjData* d) {
} }
// populate colind // populate colind
memcpy(remaining, rownnz, nv * sizeof(int)); mju_copyInt(remaining, rownnz, nv);
for (int i = nv - 1; i >= 0; i--) { for (int i = nv - 1; i >= 0; i--) {
// init at diagonal // init at diagonal
remaining[i]--; remaining[i]--;
+1 -2
View File
@@ -16,7 +16,6 @@
#include <stdio.h> #include <stdio.h>
#include <stddef.h> #include <stddef.h>
#include <string.h>
#include <mujoco/mjdata.h> #include <mujoco/mjdata.h>
#include <mujoco/mjmacro.h> #include <mujoco/mjmacro.h>
@@ -76,7 +75,7 @@ int mj_floodFill(int* island, int nr, const int* rownnz, const int* rowadr, cons
island[v] = nisland; island[v] = nisland;
// push adjacent vertices onto stack // push adjacent vertices onto stack
memcpy(stack + nstack, colind + rowadr[v], rownnz[v]*sizeof(int)); mju_copyInt(stack + nstack, colind + rowadr[v], rownnz[v]);
nstack += rownnz[v]; nstack += rownnz[v];
} }
+5 -5
View File
@@ -464,7 +464,7 @@ void mj_solPGS(const mjModel* m, mjData* d, int maxiter) {
} }
// process state // process state
memcpy(oldstate, d->efc_state, nefc*sizeof(int)); mju_copyInt(oldstate, d->efc_state, nefc);
int nactive = dualState(m, d); int nactive = dualState(m, d);
int nchange = 0; int nchange = 0;
for (int i=0; i < nefc; i++) { for (int i=0; i < nefc; i++) {
@@ -680,7 +680,7 @@ void mj_solNoSlip(const mjModel* m, mjData* d, int maxiter) {
} }
// process state // process state
memcpy(oldstate, d->efc_state, nefc*sizeof(int)); mju_copyInt(oldstate, d->efc_state, nefc);
int nactive = dualState(m, d); int nactive = dualState(m, d);
int nchange = 0; int nchange = 0;
for (int i=0; i < nefc; i++) { for (int i=0; i < nefc; i++) {
@@ -1314,7 +1314,7 @@ static void HessianCone(const mjModel* m, mjData* d, mjCGContext* ctx) {
for (int r=0; r < dim; r++) { for (int r=0; r < dim; r++) {
// copy data for this row // copy data for this row
mju_copy(LTJ_row, LTJ+r*nnz, nnz); mju_copy(LTJ_row, LTJ+r*nnz, nnz);
memcpy(LTJ_ind, d->efc_J_colind+d->efc_J_rowadr[i+r], nnz*sizeof(int)); mju_copyInt(LTJ_ind, d->efc_J_colind+d->efc_J_rowadr[i+r], nnz);
// update // update
mju_cholUpdateSparse(ctx->Hcone, LTJ_row, nv, 1, mju_cholUpdateSparse(ctx->Hcone, LTJ_row, nv, 1,
@@ -1476,7 +1476,7 @@ static void HessianIncremental(const mjModel* m, mjData* d,
// scale vec, copy colind // scale vec, copy colind
mju_scl(vec, d->efc_J+adr, mju_sqrt(d->efc_D[i]), nnz); mju_scl(vec, d->efc_J+adr, mju_sqrt(d->efc_D[i]), nnz);
memcpy(vec_ind, d->efc_J_colind+adr, nnz*sizeof(int)); mju_copyInt(vec_ind, d->efc_J_colind+adr, nnz);
// sparse update // sparse update
rank = mju_cholUpdateSparse(ctx->H, vec, nv, flag_update, rank = mju_cholUpdateSparse(ctx->H, vec, nv, flag_update,
@@ -1563,7 +1563,7 @@ static void mj_solCGNewton(const mjModel* m, mjData* d, int maxiter, int flg_New
mju_copy(gradold, ctx.grad, nv); mju_copy(gradold, ctx.grad, nv);
mju_copy(Mgradold, ctx.Mgrad, nv); mju_copy(Mgradold, ctx.Mgrad, nv);
} }
memcpy(oldstate, d->efc_state, nefc*sizeof(int)); mju_copyInt(oldstate, d->efc_state, nefc);
mjtNum oldcost = ctx.cost; mjtNum oldcost = ctx.cost;
// update // update
+1 -3
View File
@@ -14,8 +14,6 @@
#include "engine/engine_support.h" #include "engine/engine_support.h"
#include <string.h>
#include <mujoco/mjdata.h> #include <mujoco/mjdata.h>
#include <mujoco/mjmacro.h> #include <mujoco/mjmacro.h>
#include <mujoco/mjmodel.h> #include <mujoco/mjmodel.h>
@@ -1161,7 +1159,7 @@ void mj_copyM2DSparse(const mjModel* m, mjData* d, mjtNum* dst, const mjtNum* sr
// init remaining // init remaining
int* remaining = mj_stackAllocInt(d, nv); int* remaining = mj_stackAllocInt(d, nv);
memcpy(remaining, d->D_rownnz, nv * sizeof(int)); mju_copyInt(remaining, d->D_rownnz, nv);
// copy data // copy data
for (int i = nv - 1; i >= 0; i--) { for (int i = nv - 1; i >= 0; i--) {
+2 -6
View File
@@ -238,9 +238,7 @@ mjtNum mju_normalize4(mjtNum vec[4]) {
// res = 0 // res = 0
void mju_zero(mjtNum* res, int n) { void mju_zero(mjtNum* res, int n) {
if (n > 0) { memset(res, 0, n*sizeof(mjtNum));
memset(res, 0, n*sizeof(mjtNum));
}
} }
@@ -256,9 +254,7 @@ void mju_fill(mjtNum* res, mjtNum val, int n) {
// res = vec // res = vec
void mju_copy(mjtNum* res, const mjtNum* vec, int n) { void mju_copy(mjtNum* res, const mjtNum* vec, int n) {
if (n > 0) { memcpy(res, vec, n*sizeof(mjtNum));
memcpy(res, vec, n*sizeof(mjtNum));
}
} }
+8 -3
View File
@@ -1209,9 +1209,14 @@ int mju_isZero(mjtNum* vec, int n) {
// set integer vector to 0 // set integer vector to 0
void mju_zeroInt(int* res, int n) { void mju_zeroInt(int* res, int n) {
if (n > 0) { memset(res, 0, n*sizeof(int));
memset(res, 0, n*sizeof(int)); }
}
// copy int vector vec into res
void mju_copyInt(int* res, const int* vec, int n) {
memcpy(res, vec, n*sizeof(int));
} }
+3
View File
@@ -122,6 +122,9 @@ MJAPI int mju_isZero(mjtNum* vec, int n);
// set integer vector to 0 // set integer vector to 0
MJAPI void mju_zeroInt(int* res, int n); MJAPI void mju_zeroInt(int* res, int n);
// copy int vector vec into res
MJAPI void mju_copyInt(int* res, const int* vec, int n);
// standard normal random number generator (optional second number) // standard normal random number generator (optional second number)
MJAPI mjtNum mju_standardNormal(mjtNum* num2); MJAPI mjtNum mju_standardNormal(mjtNum* num2);
+1 -2
View File
@@ -16,7 +16,6 @@
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <mujoco/mjdata.h> #include <mujoco/mjdata.h>
#include <mujoco/mjmacro.h> #include <mujoco/mjmacro.h>
@@ -592,7 +591,7 @@ void mju_factorLUSparse(mjtNum* LU, int n, int* scratch,
int* remaining = scratch; int* remaining = scratch;
// set remaining = rownnz // set remaining = rownnz
memcpy(remaining, rownnz, n*sizeof(int)); mju_copyInt(remaining, rownnz, n);
// diagonal elements (i,i) // diagonal elements (i,i)
for (int i=n-1; i >= 0; i--) { for (int i=n-1; i >= 0; i--) {
+3 -3
View File
@@ -273,8 +273,8 @@ int mju_combineSparse(mjtNum* dst, const mjtNum* src, mjtNum a, mjtNum b,
// copy dst into buf // copy dst into buf
if (dst_nnz) { if (dst_nnz) {
memcpy(buf, dst, dst_nnz*sizeof(mjtNum)); mju_copy(buf, dst, dst_nnz);
memcpy(buf_ind, dst_ind, dst_nnz*sizeof(int)); mju_copyInt(buf_ind, dst_ind, dst_nnz);
} }
// prepare to merge buf and src into dst // prepare to merge buf and src into dst
@@ -599,7 +599,7 @@ void mju_sqrMatTDSparse(mjtNum* res, const mjtNum* mat, const mjtNum* matT,
// if rowsuper, use the previous row sparsity structure // if rowsuper, use the previous row sparsity structure
if (rowsuperT && i > 0 && rowsuperT[i-1]) { if (rowsuperT && i > 0 && rowsuperT[i-1]) {
res_rownnz[i] = res_rownnz[i-1]; res_rownnz[i] = res_rownnz[i-1];
memcpy(cols, res_colind+res_rowadr[i-1], res_rownnz[i]*sizeof(int)); mju_copyInt(cols, res_colind+res_rowadr[i-1], res_rownnz[i]);
} }
// iterate through each row of M' // iterate through each row of M'