1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
|
#include "CArray.hpp"
#include "Array.hpp"
#include "Interpreter.hpp"
#include "Algorithms.hpp"
#include "Math.hpp"
#ifdef _WIN32
#define EXPORT __declspec(dllexport)
//extern "C" __declspec(dllexport) void* __cxa_allocate_exception(unsigned);
//extern "C" __declspec(dllexport) void *__cxa_allocate_exception(size_t thrown_size) throw();
#else
#define EXPORT
#endif
// Copy on write is causing a problem now...
// if dp is set to the const pointer, then
// doing a write into the array may change
// two arrays. For example:
// A = zeros(1,100);
// B = A;
// for i=1:100; A(i) = i; end;
//
struct CArray
{
size_t rows;
size_t cols;
void *dp;
Array sp;
int typecode;
bool valid_for_writes;
};
struct CFunc
{
FuncPtr fp;
bool scalar;
void *sp;
};
static int ByteCount(int typecode)
{
switch (typecode)
{
case CArray_Bool : return sizeof(bool);
case CArray_Float : return sizeof(float);
case CArray_Double : return sizeof(double);
default:
throw Exception("Unknown typecode");
}
}
CArray* cast(void*p)
{
return ((CArray*)p);
}
extern "C" EXPORT
void* carray_read_ptr(void* p)
{
CArray* q = cast(p);
return q->dp;
}
void validate_writes(CArray* q)
{
q->dp = q->sp.getVoidPointer();
q->valid_for_writes = true;
}
extern "C" EXPORT
void* carray_write_ptr(void* p)
{
CArray* q = cast(p);
if (!q->valid_for_writes) validate_writes(q);
return q->dp;
}
extern "C" EXPORT
void* carray_scalar(double data, int typecode)
{
CArray *p = new CArray;
p->rows = 1;
p->cols = 1;
p->sp = Array::create((DataClass)(typecode),NTuple(1,1));
p->dp = p->sp.getVoidPointer();
switch (typecode)
{
case CArray_Bool:
((bool*)(p->dp))[0] = data;
break;
case CArray_Float:
((float*)(p->dp))[0] = data;
break;
case CArray_Double:
((double*)(p->dp))[0] = data;
break;
default:
;
}
p->valid_for_writes = true;
p->typecode = typecode;
return p;
}
extern "C" EXPORT
void* carray_create(void* interp, double rows, double cols, int typecode, bool *flag)
{
if (*flag) return NULL;
try
{
CArray *p = new CArray;
p->rows = rows;
p->cols = cols;
p->sp = Array::create((DataClass)(typecode),NTuple(rows,cols));
p->dp = p->sp.getVoidPointer();
p->valid_for_writes = true;
p->typecode = typecode;
return p;
}
catch (Exception &e)
{
reinterpret_cast<Interpreter*>(interp)->setLastErrorString(e.msg());
*flag = true;
}
}
static void* carray_capture(const Array &a)
{
CArray *p = new CArray;
p->sp = a;
p->rows = a.rows();
p->cols = a.cols();
p->dp = (void*) a.getConstVoidPointer();
p->valid_for_writes = false;
p->typecode = a.dataClass();
return p;
}
void update_cache(void* arg)
{
CArray *cp = cast(arg);
cp->rows = cp->sp.rows();
cp->cols = cp->sp.cols();
cp->dp = (void*) cp->sp.getConstVoidPointer();
cp->valid_for_writes = false;
cp->typecode = cp->sp.dataClass();
}
extern "C" EXPORT
void* carray_empty()
{
CArray *cp = new CArray;
cp->sp = EmptyConstructor();
return cp;
}
extern "C" EXPORT
void* carray_copy(void* arg)
{
return new CArray(*((CArray*)arg));
}
extern "C" EXPORT
bool carray_download_function(void *interp, void *dst, const char *name)
{
try
{
Interpreter *eval = (Interpreter*) interp;
FuncPtr fp;
if (!eval->getContext()->lookupFunction(name,fp))
throw Exception("Unable to find function");
fp->updateCode(eval);
CFunc *cp = new CFunc;
cp->fp = fp;
*((FunctionDef**)dst) = (FunctionDef*)fp;
return true;
}
catch (Exception &e)
{
Interpreter *eval = (Interpreter*) interp;
eval->setLastErrorString(e.msg());
return false;
}
}
extern "C" EXPORT
void* carray_invoke_1(void* interp, void* func, void* arg, bool *flag)
{
if (*flag) return NULL;
Interpreter *eval = (Interpreter*) interp;
try
{
FunctionDef *fp = (FunctionDef*) func;
ArrayVector args;
CArray *a1 = cast(arg);
args.push_back(a1->sp);
ArrayVector ret = fp->evaluateFunc(eval,args,1);
if (ret.size() > 0)
return carray_capture(ret[0]);
else
return carray_empty();
}
catch (Exception &e)
{
eval->setLastErrorString(e.msg());
*flag = true;
}
}
extern "C" EXPORT
void* carray_invoke_2(void* interp, void* func, void* arg1, void* arg2, bool *flag)
{
if (*flag) return NULL;
Interpreter *eval = (Interpreter*) interp;
try
{
FunctionDef *fp = (FunctionDef*) func;
ArrayVector arg;
CArray *a1 = cast(arg1);
CArray *a2 = cast(arg2);
arg.push_back(a1->sp);
arg.push_back(a2->sp);
ArrayVector ret = fp->evaluateFunc(eval,arg,1);
if (ret.size() > 0)
return carray_capture(ret[0]);
else
return carray_empty();
}
catch (Exception &e)
{
eval->setLastErrorString(e.msg());
*flag = true;
}
}
extern "C" EXPORT
bool carray_download_scalar(void *interp, void* dst, const char *name, int typecode)
{
try
{
Interpreter *eval = (Interpreter*) interp;
ArrayReference ptr(eval->getContext()->lookupVariable(name));
if (!ptr.valid())
{
eval->getContext()->insertVariable(name,Array((DataClass)(typecode),NTuple(1,1)));
ptr = eval->getContext()->lookupVariable(name);
}
if (!ptr->isScalar()) throw Exception("Ugh");
if (ptr->dataClass() != typecode) throw Exception("Ugh");
memcpy(dst,ptr->getVoidPointer(),ByteCount(typecode));
return true;
}
catch (Exception &e)
{
Interpreter *eval = (Interpreter*) interp;
eval->setLastErrorString(e.msg());
return false;
}
}
extern "C" EXPORT
bool carray_download_array(void *interp, void *data, const char *name, int typecode)
{
try
{
Interpreter *eval = (Interpreter*) interp;
ArrayReference ptr(eval->getContext()->lookupVariable(name));
if (!ptr.valid())
{
eval->getContext()->insertVariable(name,Array((DataClass)(typecode),NTuple(1,1)));
ptr = eval->getContext()->lookupVariable(name);
}
if (ptr->dataClass() != (DataClass)(typecode)) throw Exception("Ugh");
CArray *cp = cast(data);
cp->sp = *ptr;
update_cache(data);
return true;
}
catch (Exception &e)
{
Interpreter *eval = (Interpreter*) interp;
eval->setLastErrorString(e.msg());
return false;
}
}
extern "C" EXPORT
bool carray_upload_scalar(void *interp, void* data, const char *name,
int typecode)
{
Interpreter *eval = (Interpreter*) interp;
try
{
ArrayReference ptr(eval->getContext()->lookupVariable(name));
if (!ptr.valid())
{
eval->getContext()->insertVariable(name,Array((DataClass)(typecode),NTuple(1,1)));
ptr = eval->getContext()->lookupVariable(name);
}
if (!ptr->isScalar()) throw Exception("Internal JIT failure!");
if (ptr->dataClass() != (DataClass)(typecode))
throw Exception("Internal JIT failure!");
memcpy(ptr->getVoidPointer(),data,ByteCount(typecode));
return true;
}
catch (Exception &e)
{
eval->setLastErrorString(e.msg());
return false;
}
}
extern "C" EXPORT
bool carray_upload_array(void *interp, void *data, const char *name)
{
Interpreter *eval = (Interpreter*) interp;
try
{
ArrayReference ptr(eval->getContext()->lookupVariable(name));
CArray *cp = cast(data);
if (!ptr.valid())
{
eval->getContext()->insertVariable(name,cp->sp);
return true;
}
*ptr = cp->sp;
return true;
}
catch (Exception &e)
{
eval->setLastErrorString(e.msg());
return false;
}
}
// Delete an array
extern "C" EXPORT
void carray_free(void* arg)
{
delete (cast(arg));
}
// Get the number of rows in the array
extern "C" EXPORT
double carray_rows(void* arg)
{
return (cast(arg)->rows);
}
// Get the number of cols in the array
extern "C" EXPORT
double carray_cols(void* arg)
{
return (cast(arg)->cols);
}
extern "C" EXPORT
bool carray_set_ss(void* interp, void *arg, double row, double col, double val)
{
try
{
CArray *cp = cast(arg);
if (!cp->valid_for_writes) validate_writes(cp);
if ((row < 1) || (col < 1))
{
Interpreter *eval = (Interpreter*) interp;
eval->setLastErrorString("index values must be >= 1");
return false;
}
if ((row > cp->rows) || (col > cp->cols))
{
cp->sp.set(NTuple(row,col),Array(val));
update_cache(arg);
validate_writes(cast(arg));
return true;
}
size_t irow = (size_t) row-1;
size_t icol = (size_t) col-1;
switch (cp->typecode)
{
case CArray_Bool:
((bool*)(cp->dp))[irow+icol*cp->rows] = val;
return true;
case CArray_Float:
((float*)(cp->dp))[irow+icol*cp->rows] = val;
return true;
case CArray_Double:
((double*)(cp->dp))[irow+icol*cp->rows] = val;
return true;
default:
return false;
}
}
catch (Exception &e)
{
Interpreter *eval = (Interpreter*) interp;
eval->setLastErrorString(e.msg());
}
return false;
}
extern "C" EXPORT
bool carray_set_s(void* interp, void *arg, double row, double val)
{
try
{
CArray *cp = cast(arg);
if (!cp->valid_for_writes) validate_writes(cp);
if (row < 1)
{
Interpreter *eval = (Interpreter*) interp;
eval->setLastErrorString("index values must be >= 1");
return false;
}
if (row > cp->rows*cp->cols)
{
cp->sp.set(row,Array(val));
update_cache(arg);
validate_writes(cast(arg));
return true;
}
size_t irow = (size_t) row-1;
switch (cp->typecode)
{
case CArray_Bool:
((bool*)(cp->dp))[irow] = val;
return true;
case CArray_Float:
((float*)(cp->dp))[irow] = val;
return true;
case CArray_Double:
((double*)(cp->dp))[irow] = val;
return true;
default:
return false;
}
}
catch (Exception &e)
{
Interpreter *eval = (Interpreter*) interp;
eval->setLastErrorString(e.msg());
}
return false;
}
extern "C" EXPORT
bool carray_set_a(void* interp, void* arg, void* ndx, void *val)
{
try
{
CArray *ap = cast(arg);
CArray *nd = cast(ndx);
CArray *vc = cast(val);
if (!ap->valid_for_writes) validate_writes(ap);
ap->sp.set(nd->sp,vc->sp);
update_cache(ap);
return true;
}
catch (Exception &e)
{
Interpreter *eval = (Interpreter*) interp;
eval->setLastErrorString(e.msg());
}
return false;
}
extern "C" EXPORT
bool carray_set_aa(void* interp, void* arg, void* ndxr, void* ndxc, void *val)
{
try
{
CArray *ap = cast(arg);
CArray *ndr = cast(ndxr);
CArray *ndc = cast(ndxc);
CArray *vc = cast(val);
if (!ap->valid_for_writes) validate_writes(ap);
ArrayVector ndx;
ndx.push_back(ndr->sp);
ndx.push_back(ndc->sp);
ap->sp.set(ndx,vc->sp);
update_cache(ap);
return true;
}
catch (Exception &e)
{
Interpreter *eval = (Interpreter*) interp;
eval->setLastErrorString(e.msg());
}
return false;
}
extern "C" EXPORT
void* carray_get_a(void* interp, void* arg, void* ndx, bool *flag)
{
if (*flag) return NULL;
try
{
CArray *ap = cast(arg);
CArray *nd = cast(ndx);
return carray_capture(ap->sp.get(nd->sp));
}
catch (Exception &e)
{
*flag = true;
Interpreter *eval = (Interpreter*) interp;
eval->setLastErrorString(e.msg());
return NULL;
}
}
extern "C" EXPORT
void* carray_get_aa(void* interp, void* arg, void* rndx, void* cndx, bool *flag)
{
if (*flag) return NULL;
try
{
CArray *ap = cast(arg);
CArray *ndr = cast(rndx);
CArray *ndc = cast(cndx);
ArrayVector ndx;
ndx.push_back(ndr->sp);
ndx.push_back(ndc->sp);
return carray_capture(ap->sp.get(ndx));
}
catch (Exception &e)
{
*flag = true;
Interpreter *eval = (Interpreter*) interp;
eval->setLastErrorString(e.msg());
return NULL;
}
}
extern "C" EXPORT
double carray_get_ss(void* interp, void* arg, double row, double col, bool *flag)
{
if (*flag) return NULL;
try
{
CArray *cp = cast(arg);
if ((row < 1) || (col < 1) || (row > cp->rows) || (col > cp->cols))
{
*flag = true;
Interpreter *eval = (Interpreter*) interp;
eval->setLastErrorString("out of range");
return 0;
}
size_t irow = (size_t) row-1;
size_t icol = (size_t) col-1;
switch (cp->typecode)
{
case CArray_Bool:
return ((bool*)(cp->dp))[irow+icol*cp->rows];
case CArray_Float:
return ((float*)(cp->dp))[irow+icol*cp->rows];
case CArray_Double:
return ((double*)(cp->dp))[irow+icol*cp->rows];
}
}
catch (Exception &e)
{
*flag = true;
Interpreter *eval = (Interpreter*) interp;
eval->setLastErrorString(e.msg());
return 0;
}
}
extern "C" EXPORT
double carray_get_s(void* interp, void* arg, double row, bool *flag)
{
if (*flag) return NULL;
try
{
CArray *cp = cast(arg);
if ((row < 1) || (row > (cp->rows)*(cp->cols)))
{
*flag = true;
Interpreter *eval = (Interpreter*) interp;
eval->setLastErrorString("out of range");
return 0;
}
size_t irow = (size_t) row - 1;
switch (cp->typecode)
{
case CArray_Bool:
return ((bool*)(cp->dp))[irow];
case CArray_Float:
return ((float*)(cp->dp))[irow];
case CArray_Double:
return ((double*)(cp->dp))[irow];
}
}
catch (Exception &e)
{
*flag = true;
Interpreter *eval = (Interpreter*) interp;
eval->setLastErrorString(e.msg());
return 0;
}
}
extern "C" EXPORT
bool carray_duplicate(void *interp, void *a, void *b)
{
try
{
CArray *ap = cast(a);
CArray *bp = cast(b);
ap->sp = bp->sp;
update_cache(ap);
return true;
}
catch (Exception &e)
{
reinterpret_cast<Interpreter*>(interp)->setLastErrorString(e.msg());
return false;
}
}
extern "C" EXPORT
void* carray_colon(void *interp, double a, double b, bool *flag)
{
if (*flag) return NULL;
try
{
CArray *cp = cast(carray_empty());
cp->sp = RangeConstructor(a,1,b,false);
update_cache(cp);
return cp;
}
catch (Exception &e)
{
*flag = true;
reinterpret_cast<Interpreter*>(interp)->setLastErrorString(e.msg());
return NULL;
}
}
extern "C" EXPORT
void* carray_dcolon(void *interp, double a, double b, double c, bool *flag)
{
if (*flag) return NULL;
try
{
CArray *cp = cast(carray_empty());
cp->sp = RangeConstructor(a,b,c,false);
update_cache(cp);
return cp;
}
catch (Exception &e)
{
*flag = true;
reinterpret_cast<Interpreter*>(interp)->setLastErrorString(e.msg());
return NULL;
}
}
extern "C" EXPORT
bool carray_any(void *interp, void *p, bool *flag)
{
if (*flag) return NULL;
try
{
CArray *pp = cast(p);
return !RealAllZeros(pp->sp);
}
catch (Exception &e)
{
*flag = true;
reinterpret_cast<Interpreter*>(interp)->setLastErrorString(e.msg());
return false;
}
}
extern "C" EXPORT
bool carray_all(void *interp, void *p, bool *flag)
{
if (*flag) return NULL;
try
{
CArray *pp = cast(p);
return RealAllNonZeros(pp->sp);
}
catch (Exception &e)
{
*flag = true;
reinterpret_cast<Interpreter*>(interp)->setLastErrorString(e.msg());
return false;
}
}
#define WrapUnaryOp(wrapped,func) \
extern "C" EXPORT \
void* wrapped(void* interp, void *a, bool *flag) \
{ \
if (*flag) return NULL; \
try \
{ \
CArray *ap = cast(a); \
CArray *cp = cast(carray_empty()); \
cp->sp = func(ap->sp); \
update_cache(cp); \
return cp; \
} \
catch (Exception&e) \
{ \
reinterpret_cast<Interpreter*>(interp)->setLastErrorString(e.msg()); \
*flag = true; \
return NULL; \
} \
}
WrapUnaryOp(carray_neg,Negate);
WrapUnaryOp(carray_pos,Plus);
WrapUnaryOp(carray_not,Not);
WrapUnaryOp(carray_transpose,Hermitian);
WrapUnaryOp(carray_dottranspose,Transpose);
#define WrapOp(wrapped,func) \
extern "C" EXPORT \
void* wrapped(void* interp, void *a, void *b, bool *flag) \
{ \
if (*flag) return NULL; \
try \
{ \
CArray *ap = cast(a); \
CArray *bp = cast(b); \
CArray *cp = cast(carray_empty()); \
cp->sp = func(ap->sp,bp->sp); \
update_cache(cp); \
return cp; \
} \
catch (Exception &e) \
{ \
*flag = true; \
reinterpret_cast<Interpreter*>(interp)->setLastErrorString(e.msg()); \
return NULL; \
} \
}
Array HCat(const Array& A, const Array& B)
{
ArrayVector p;
p.push_back(A);
p.push_back(B);
return NCat(p,1);
}
Array VCat(const Array& A, const Array& B)
{
ArrayVector p;
p.push_back(A);
p.push_back(B);
return NCat(p,0);
}
WrapOp(carray_hcat,HCat);
WrapOp(carray_vcat,VCat);
WrapOp(carray_add,Add);
WrapOp(carray_pow,Power);
WrapOp(carray_dpow,DotPower);
WrapOp(carray_sub,Subtract);
WrapOp(carray_times,Multiply);
WrapOp(carray_dtimes,DotMultiply);
WrapOp(carray_rdiv,RightDivide);
WrapOp(carray_drdiv,DotRightDivide);
WrapOp(carray_ldiv,LeftDivide);
WrapOp(carray_dldiv,DotLeftDivide);
WrapOp(carray_or,Or);
WrapOp(carray_and,And);
WrapOp(carray_lt,LessThan);
WrapOp(carray_le,LessEquals);
WrapOp(carray_gt,GreaterThan);
WrapOp(carray_ge,GreaterEquals);
WrapOp(carray_eq,Equals);
WrapOp(carray_neq,NotEquals);
|