Commit 60c5fb90 authored by A. Borzov's avatar A. Borzov
Browse files

добавил n-мерные массивы. Теперь можно добавлять умножение матриц.

parent cf8a7bf1
#include "Matrix.h"
#include "Net.h"
#include "Funcs.h"
#include "VM.h"
#include <IpBlas.hpp>
using namespace Ipopt;
void xpy(DoubleArr& x, DoubleArr& y, Net& net) {
int xw = x.size;
void Matrix::plus(const Matrix &y, Net &net) {
Matrix &x = *this;
int xw = x.size();
int rank = net.rank;
int size = net.size;
int displs[size];
initDispls(displs, size, 1, xw);
int start = displs[rank];
int finish = ((rank == size - 1) ? xw : displs[mod(rank + 1, size)]);
IpBlasDaxpy(finish - start, 1.0, x.ptr + start, 1, y.ptr + start, 1);
IpBlasDaxpy(finish - start, 1.0, y.ptr + start, 1, x.ptr + start, 1);
net.MPI_ALLGATHERV(
(char *) (y.ptr + displs[rank]), xw,(char *) y.ptr, displs,MPI_DOUBLE);
(char *) (x.ptr + displs[rank]), xw, (char *) x.ptr, displs, MPI_DOUBLE);
}
/*int lastRowNum = ((rank == size - 1) ? xh * xw : displs[mod(rank + 1, size)]) / xw;
for (int i = displs[rank] / xw; i < lastRowNum; i++) {
for (int j = 0; j < xw; j++) {
x[i * xw + j] = x[i * xw + j] + y[i * xw + j];
Matrix::Matrix(std::vector<int> &dimensions, double *ptr) {
this->dimensions = dimensions;//std::move(dimensions);
this->ptr = ptr;
}
int Matrix::size() {
int sum = 0;
for (int i = 0; i < dimensions.size(); i++) {
sum += dimensions[i];
}
}*/
\ No newline at end of file
return sum;
}
Matrix::~Matrix() {
delete[] ptr;
}
#ifndef UNTITLED_MATRIX_H
#define UNTITLED_MATRIX_H
#ifndef KOTLIN_CPP_MATRIX_H
#define KOTLIN_CPP_MATRIX_H
#include <vector>
#include "Net.h"
struct DoubleArr;
class Matrix {
public:
Matrix(std::vector<int>& dimensions, double* ptr);
std::vector<int> dimensions;
double *ptr;
int size();
~Matrix();
void plus(const Matrix& b, Net& net);
};
void xpy(DoubleArr& x, DoubleArr& y, Net& net);
#endif //UNTITLED_MATRIX_H
#endif //KOTLIN_CPP_MATRIX_H
#include "VM.h"
#include "Matrix.h"
#include <android/log.h>
#include <IpBlas.hpp>
......@@ -54,7 +53,7 @@ void VM::run() {
while (++cursor < operations.size()) {
Row &row = operations[cursor];
std::vector<Variable *> args;
std::vector < Variable * > args;
for (int it : row.argsIds) {
if (it < 0) {
args.emplace_back(&constants[-it - 1]);
......@@ -208,32 +207,47 @@ void VM::Not(std::vector<Variable *> &args) {
}
void VM::createDoubleArray(std::vector<Variable *> &args) {
int argCursor = 0;
int dimCount = args[argCursor]->i;
argCursor++;
std::vector<int> dims;
for (int i = 0; i < dimCount; i++) {
dims.emplace_back(args[argCursor]->i);
argCursor++;
}
int arraySize = args[argCursor]->i;
argCursor++;
double *arr = new double[arraySize];
for (int i = 0; i < arraySize; i++) {
auto *it = args[argCursor];
argCursor++;
arr[i] = it->d;
}
Variable ptr;
ptr.darr = {args[0]->i, new double[args[0]->i]};
ptr.darr = new Matrix(dims, arr);
realStack.emplace_back(ptr);
for (int i = 1; i < args.size(); i++) {
auto *it = args[i];
ptr.darr.ptr[i - 1] = it->d;
}
}
void VM::deleteDoubleArray(std::vector<Variable *> args) {
delete[] args[0]->darr.ptr;
delete args[0]->darr;
}
void VM::printDoubleArray(std::vector<Variable *> args) {
DoubleArr a = args[0]->darr;
for (int i = 0; i < a.size; i++) {
Matrix &a = *(args[0]->darr);
for (int i = 0; i < a.size(); i++) {
printf("%lf; ", a.ptr[i]);
}
printf("\n");
}
void VM::sumDArrDArr(std::vector<Variable *> &args) {
//TODO: xpy
DoubleArr a = args[0]->darr;
DoubleArr b = args[1]->darr;
xpy(b, a, (*net));
Matrix &a = *(args[0]->darr);
Matrix &b = *(args[1]->darr);
a.plus(b, *net);
//Ipopt::IpBlasDaxpy(b.size, 1.0, b.ptr, 1, a.ptr, 1);
/*
for (int i = 0; i < a.size; i++) {
......
......@@ -5,23 +5,21 @@
#include <vector>
#include <string>
#include "Net.h"
#include "Matrix.h"
//#include "Task.h"
//class Net;
struct DoubleArr {
int size;
double *ptr;
} typedef DoubleArr;
union Variable {
int64_t data;
int i;
double d;
bool b;
DoubleArr darr;
Matrix* darr;
Variable() {
}
Variable(int64_t data) {
this->data = data;
}
......@@ -32,13 +30,15 @@ struct Row {
std::vector<int> argsIds;
Row(int opId, std::vector<int> argsIds) {
this->opId = opId, this->argsIds = std::move(argsIds);
}//FIXME:убрать, у структуры есть init list
this->opId = opId;
this->argsIds = std::move(argsIds);
}
} typedef Row;
struct Client {
std::string ip;
int port;
Client(std::string ip, int port) {
this->ip = ip;
this->port = port;
......@@ -50,11 +50,13 @@ public:
VM(jbyte *taskBody);
void run();
~VM() {
if(net != nullptr) {
if (net != nullptr) {
delete net;
}
}
private:
template<typename T>
T readNext(jbyte *&readCursor) {
......@@ -68,7 +70,7 @@ private:
std::vector<Row> operations;
std::vector<Client> clients;
int rank;
Net* net = nullptr;
Net *net = nullptr;
int cursor = -1;
inline void doublePlus(std::vector<Variable *> &vector);
......@@ -103,7 +105,7 @@ private:
inline void printDoubleArray(std::vector<Variable *> args);
inline void sumDArrDArr(std::vector<Variable *>& args);
inline void sumDArrDArr(std::vector<Variable *> &args);
void intLess(std::vector<Variable *> &args);
......
......@@ -4,7 +4,6 @@ import ru.nsu.fit.borzov.kotlin_cpp.edge.lang.OpEnum.*
import ru.nsu.fit.borzov.kotlin_cpp.edge.machine.MVariable
import ru.nsu.fit.borzov.kotlin_cpp.edge.user.Client
import java.io.ByteArrayOutputStream
import java.lang.RuntimeException
import java.nio.ByteBuffer
import java.nio.ByteOrder
......@@ -48,8 +47,8 @@ class Task(val clients: List<Client>) {
internal fun _getConstBool(b: Boolean): LCBool {
val toFind = LCBool(b, -(constants.size + 1), this);
val result = constants.getOrPut(if(b) 1 else 0, { toFind });
return when(result) {
val result = constants.getOrPut(if (b) 1 else 0, { toFind });
return when (result) {
is LCBool -> result;
is LCInt -> LCBool(result.value != 0, result.id, result.task)
else -> throw RuntimeException("Error getting constant.");
......@@ -109,10 +108,9 @@ class Task(val clients: List<Client>) {
}
fun toByteArray(num: Int): ByteArray {
val bf = ByteArrayOutputStream()
val constantsList = mutableListOf<MVariable>()
constants.forEach { (k, v) ->
constants.forEach { (_, v) ->
constantsList.add(-v.id - 1, MVariable(v))
}
......@@ -158,8 +156,21 @@ class Task(val clients: List<Client>) {
return bf.toByteArray()
}
fun createDoubleArray(vararg elements: Double): LDoubleArray {
val args = mutableListOf(_getConstInt(elements.size).id)
fun dim(vararg dim: Int): IntArray {
return dim;
}
fun array(vararg elements: Double): DoubleArray {
return elements;
}
fun createDoubleArray(dim: IntArray, elements: DoubleArray): LDoubleArray {
val args = mutableListOf(_getConstInt(dim.size).id)
dim.forEach {
args.add(_getConstInt(it).id)
}
args.add(_getConstInt(elements.size).id)
elements.forEach {
args.add(_getConstDouble(it).id)
}
......@@ -169,6 +180,14 @@ class Task(val clients: List<Client>) {
return toReturn;
}
fun createDoubleArray(width: Int, height: Int, elements: DoubleArray): LDoubleArray {
return createDoubleArray(dim(width, height), elements)
}
fun createDoubleArray(vararg elements: Double): LDoubleArray {
return createDoubleArray(dim(elements.size), elements)
}
/*
fun createDoubleArray(arr: DoubleArray) : LDoubleArray {
val args = mutableListOf(_getConstInt(arr.size))
......@@ -203,6 +222,11 @@ class Task(val clients: List<Client>) {
task.operations.add(CodeRow(SumDoubleArrArr.id, mutableListOf(this.id, b.id)));
return this
}
fun LDoubleArray.times(b: LDoubleArray): LDoubleArray {
task.operations.add(CodeRow(TimesDoubleArrArr.id, mutableListOf(this.id, b.id)));
return this
}
}
private fun ByteArrayOutputStream.writeInt(i: Int): ByteArrayOutputStream {
......@@ -232,6 +256,7 @@ enum class OpEnum(val id: Int) {
PrintDoubleArray(15),
SumDoubleArrArr(16),
IntLess(17),
IntPlus(18)
IntPlus(18),
TimesDoubleArrArr(19)
//DoubleArrayGet(15),
}
\ No newline at end of file
......@@ -20,6 +20,33 @@ fun sumArrs(): Task {
return task
}
fun times(): Task {
val task = Task(clients)
with(task) {
val i = createInt(0)
val arr1 =
createDoubleArray(
dim(2, 4), array(
1.0, 2.0,
3.0, 4.0,
5.0, 6.0,
70.0, 8.0
)
)
val arr2 =
createDoubleArray(
dim(4, 2), array(
9.0, 10.0, 11.0, 12.0,
13.0, 14.0, 15.0, 16.0
)
)
arr1.times(arr2)
printDoubleArray(arr1)
}
return task
}
fun iff() {
val task = Task(clients)
with(task) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment