-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pushing all I created (will delete unnecessary files later)
- Loading branch information
1 parent
41bfc06
commit d8ba06b
Showing
8 changed files
with
195 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[ | ||
{ | ||
"index_val": 1, | ||
"my_val": 421, | ||
"their_val": 991 | ||
}, | ||
{ | ||
"index_val": 3, | ||
"my_val": 422, | ||
"their_val": 991 | ||
}, | ||
{ | ||
"index_val": 7, | ||
"my_val": 427, | ||
"their_val": 991 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"r1": [ | ||
[ | ||
1, | ||
421 | ||
], | ||
[ | ||
2, | ||
422 | ||
], | ||
[ | ||
3, | ||
422 | ||
], | ||
[ | ||
4, | ||
423 | ||
], | ||
[ | ||
7, | ||
427 | ||
] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"r2": [ | ||
[ | ||
1, | ||
991 | ||
], | ||
[ | ||
3, | ||
991 | ||
], | ||
[ | ||
5, | ||
991 | ||
], | ||
[ | ||
7, | ||
991 | ||
] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Controller, Post, Body, Res } from '@nestjs/common'; | ||
import { MpcService } from './mpc.service'; | ||
import { Response } from 'express'; | ||
|
||
@Controller('mpc') | ||
export class MpcController { | ||
constructor(private readonly mpcService: MpcService) {} | ||
|
||
@Post('run') | ||
async runMpc(@Body() body: { data1: any; data2: any }, @Res() res: Response) { | ||
try { | ||
const output = await this.mpcService.runMpc(body.data1, body.data2); | ||
res.status(200).json({ output }); | ||
} catch (error) { | ||
res.status(500).json({ message: error }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MpcService } from './mpc.service'; | ||
import { MpcController } from './mpc.controller'; | ||
|
||
@Module({ | ||
providers: [MpcService], | ||
controllers: [MpcController], | ||
}) | ||
export class MpcModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { spawn } from 'child_process'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
@Injectable() | ||
export class MpcService { | ||
async runMpc(data1: any, data2: any): Promise<string> { | ||
const filePath1 = path.join(process.cwd(), 'sample1.json'); | ||
const filePath2 = path.join(process.cwd(), 'sample2.json'); | ||
const jsonString1 = JSON.stringify({ data1 }, null, 2); | ||
const jsonString2 = JSON.stringify({ data2 }, null, 2); | ||
|
||
await fs.promises.writeFile(filePath1, jsonString1); | ||
await fs.promises.writeFile(filePath2, jsonString2); | ||
|
||
return new Promise((resolve, reject) => { | ||
const command = 'mpirun'; | ||
const args = ['-np', '3', './test_join_sail', filePath1, filePath2]; | ||
|
||
const mpcProcess = spawn(command, args); | ||
|
||
let output = ''; | ||
|
||
mpcProcess.stdout.on('data', (data) => { | ||
output += data.toString(); | ||
}); | ||
|
||
mpcProcess.stderr.on('data', (data) => { | ||
console.error(`stderr: ${data}`); | ||
}); | ||
|
||
mpcProcess.on('close', (code) => { | ||
if (code === 0) { | ||
resolve(output); | ||
} else { | ||
reject(`Process exited with code ${code}`); | ||
} | ||
}); | ||
|
||
mpcProcess.on('error', (error) => { | ||
reject(`Error: ${error.message}`); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { spawn } from 'child_process'; | ||
|
||
// Function to write JSON files | ||
const writeJsonFile = async (filePath, data) => { | ||
const jsonString = JSON.stringify(data, null, 2); | ||
await fs.promises.writeFile(filePath, jsonString); | ||
}; | ||
|
||
const runMpc = async () => { | ||
const r1 = [[1, 421], [2, 422], [3, 422], [4, 423], [7, 427]]; | ||
const data1 = { r1 }; | ||
const filePath1 = path.join("./", 'sample1.json'); | ||
const r2 = [[1, 991], [3, 991], [5, 991], [7, 991]]; | ||
const data2 = { r2 }; | ||
const filePath2 = path.join('./', 'sample2.json'); | ||
|
||
// Write the JSON files | ||
await writeJsonFile(filePath1, data1); | ||
await writeJsonFile(filePath2, data2); | ||
|
||
// Define the command and arguments | ||
const command = 'mpirun'; | ||
const args = ['-np', '3', '/Users/hishii/Desktop/MT/Secrecy/build/test_join_sail', filePath1, filePath2]; | ||
// Spawn the process | ||
const process = spawn(command, args); | ||
|
||
// Handle stdout | ||
process.stdout.on('data', (data) => { | ||
console.log(`stdout: ${data}`); | ||
}); | ||
|
||
// Handle stderr | ||
process.stderr.on('data', (data) => { | ||
console.error(`stderr: ${data}`); | ||
}); | ||
|
||
// Handle process exit | ||
process.on('close', (code) => { | ||
console.log(`Process exited with code ${code}`); | ||
}); | ||
|
||
// Handle process errors | ||
process.on('error', (error) => { | ||
console.error(`Error: ${error.message}`); | ||
}); | ||
}; | ||
|
||
// Run the MPC process | ||
runMpc().catch((error) => { | ||
console.error(`Error running MPC process: ${error.message}`); | ||
}); |