Skip to content

Commit b951997

Browse files
Changed package, added information to CODEOWNERS
1 parent 5a184db commit b951997

File tree

8 files changed

+612
-0
lines changed

8 files changed

+612
-0
lines changed

tpu/pom.xml

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2024 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xmlns="http://maven.apache.org/POM/4.0.0"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<groupId>com.example.tpu</groupId>
22+
<artifactId>gce-diregapic-samples</artifactId>
23+
<version>1.0-SNAPSHOT</version>
24+
25+
<!--
26+
The parent pom defines common style checks and testing strategies for our samples.
27+
Removing or replacing it should not affect the execution of the samples in anyway.
28+
-->
29+
<parent>
30+
<artifactId>shared-configuration</artifactId>
31+
<groupId>com.google.cloud.samples</groupId>
32+
<version>1.2.0</version>
33+
</parent>
34+
35+
<properties>
36+
<maven.compiler.source>11</maven.compiler.source>
37+
<maven.compiler.target>11</maven.compiler.target>
38+
</properties>
39+
40+
<dependencies>
41+
<dependency>
42+
<groupId>com.google.cloud</groupId>
43+
<artifactId>google-cloud-tpu</artifactId>
44+
<version>2.52.0</version>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>com.google.api</groupId>
49+
<artifactId>gax</artifactId>
50+
</dependency>
51+
52+
<!-- Test dependencies -->
53+
<dependency>
54+
<artifactId>google-cloud-storage</artifactId>
55+
<groupId>com.google.cloud</groupId>
56+
<scope>test</scope>
57+
</dependency>
58+
59+
<dependency>
60+
<artifactId>truth</artifactId>
61+
<groupId>com.google.truth</groupId>
62+
<scope>test</scope>
63+
<version>1.4.0</version>
64+
</dependency>
65+
<dependency>
66+
<artifactId>junit</artifactId>
67+
<groupId>junit</groupId>
68+
<scope>test</scope>
69+
<version>4.13.2</version>
70+
</dependency>
71+
72+
<!--
73+
JUnit Jupiter dependencies to run BeforeEach and AfterEach methods
74+
(in tandem with mvn surefire) before every test.
75+
Without these, mvn surefire skips these methods and leads to concurrency
76+
issues.
77+
-->
78+
<dependency>
79+
<groupId>org.junit.jupiter</groupId>
80+
<artifactId>junit-jupiter-engine</artifactId>
81+
<version>5.10.2</version>
82+
<scope>test</scope>
83+
</dependency>
84+
</dependencies>
85+
86+
<dependencyManagement>
87+
<dependencies>
88+
<dependency>
89+
<artifactId>libraries-bom</artifactId>
90+
<groupId>com.google.cloud</groupId>
91+
<scope>import</scope>
92+
<type>pom</type>
93+
<version>26.40.0</version>
94+
</dependency>
95+
</dependencies>
96+
</dependencyManagement>
97+
98+
</project>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package tpu;
18+
19+
//[START tpu_vm_create]
20+
21+
import com.google.cloud.tpu.v2.CreateNodeRequest;
22+
import com.google.cloud.tpu.v2.Node;
23+
import com.google.cloud.tpu.v2.TpuClient;
24+
import java.io.IOException;
25+
import java.util.concurrent.ExecutionException;
26+
27+
public class CreateTpuVm {
28+
29+
public static void main(String[] args)
30+
throws IOException, ExecutionException, InterruptedException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "YOUR_PROJECT_ID";
33+
String zone = "europe-west4-a";
34+
String tpuVmName = "YOUR_TPU_NAME";
35+
String acceleratorType = "v2-8";
36+
String version = "tpu-vm-tf-2.14.1";
37+
38+
createTpuVm(projectId, zone, tpuVmName, acceleratorType, version);
39+
}
40+
41+
// Creates a TPU VM with the specified name, zone, accelerator type, and version.
42+
public static void createTpuVm(
43+
String projectId, String zone, String tpuVmName, String acceleratorType, String version)
44+
throws IOException, ExecutionException, InterruptedException {
45+
// Initialize client that will be used to send requests. This client only needs to be created
46+
// once, and can be reused for multiple requests.
47+
try (TpuClient tpuClient = TpuClient.create()) {
48+
String parent = String.format("projects/%s/locations/%s", projectId, zone);
49+
50+
Node tpuVm = Node.newBuilder()
51+
.setName(tpuVmName)
52+
.setAcceleratorType(acceleratorType)
53+
.setRuntimeVersion(version)
54+
.build();
55+
56+
CreateNodeRequest request = CreateNodeRequest.newBuilder()
57+
.setParent(parent)
58+
.setNodeId(tpuVmName)
59+
.setNode(tpuVm)
60+
.build();
61+
62+
Node response = tpuClient.createNodeAsync(request).get();
63+
System.out.printf("TPU VM created: %s\n", response.getName());
64+
}
65+
}
66+
}
67+
//[END tpu_vm_create]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package tpu;
18+
19+
//[START tpu_vm_delete]
20+
21+
import com.google.cloud.tpu.v2.DeleteNodeRequest;
22+
import com.google.cloud.tpu.v2.NodeName;
23+
import com.google.cloud.tpu.v2.TpuClient;
24+
import java.io.IOException;
25+
import java.util.concurrent.ExecutionException;
26+
27+
public class DeleteTpuVm {
28+
29+
public static void main(String[] args)
30+
throws IOException, ExecutionException, InterruptedException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "YOUR_PROJECT_ID";
33+
String zone = "europe-west4-a";
34+
String tpuVmName = "YOUR_TPU_NAME";
35+
36+
deleteTpuVm(projectId, zone, tpuVmName);
37+
}
38+
39+
// Deletes a TPU VM with the specified name in the given project and zone.
40+
public static void deleteTpuVm(String projectId, String zone, String tpuVmName)
41+
throws IOException, ExecutionException, InterruptedException {
42+
// Initialize client that will be used to send requests. This client only needs to be created
43+
// once, and can be reused for multiple requests.
44+
try (TpuClient tpuClient = TpuClient.create()) {
45+
String nodeName = NodeName.of(projectId, zone, tpuVmName).toString();
46+
47+
DeleteNodeRequest request = DeleteNodeRequest.newBuilder().setName(nodeName).build();
48+
tpuClient.deleteNodeAsync(request).get();
49+
50+
System.out.println("TPU VM deleted");
51+
}
52+
}
53+
}
54+
//[END tpu_vm_delete]
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package tpu;
18+
19+
//[START tpu_vm_get]
20+
21+
import com.google.cloud.tpu.v2.GetNodeRequest;
22+
import com.google.cloud.tpu.v2.Node;
23+
import com.google.cloud.tpu.v2.NodeName;
24+
import com.google.cloud.tpu.v2.TpuClient;
25+
import java.io.IOException;
26+
27+
public class GetTpuVm {
28+
29+
public static void main(String[] args) throws IOException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "YOUR_PROJECT_ID";
32+
String zone = "europe-west4-a";
33+
String tpuVmName = "YOUR_TPU_NAME";
34+
35+
getTpuVm(projectId, zone, tpuVmName);
36+
}
37+
38+
// Describes a TPU VM with the specified name in the given project and zone.
39+
public static Node getTpuVm(String projectId, String zone, String tpuVmName)
40+
throws IOException {
41+
Node node;
42+
// Initialize client that will be used to send requests. This client only needs to be created
43+
// once, and can be reused for multiple requests.
44+
try (TpuClient tpuClient = TpuClient.create()) {
45+
String nodeName = NodeName.of(projectId, zone, tpuVmName).toString();
46+
47+
GetNodeRequest request = GetNodeRequest.newBuilder().setName(nodeName).build();
48+
node = tpuClient.getNode(request);
49+
}
50+
return node;
51+
}
52+
}
53+
//[END tpu_vm_get]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package tpu;
18+
19+
//[START tpu_vm_list]
20+
21+
import com.google.cloud.tpu.v2.ListNodesRequest;
22+
import com.google.cloud.tpu.v2.TpuClient;
23+
import com.google.cloud.tpu.v2.TpuClient.ListNodesPagedResponse;
24+
import java.io.IOException;
25+
26+
public class ListTpuVms {
27+
28+
public static void main(String[] args) throws IOException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "YOUR_PROJECT_ID";
31+
String zone = "europe-west4-a";
32+
33+
listTpuVms(projectId, zone);
34+
}
35+
36+
// Lists TPU VMs in the specified zone.
37+
public static ListNodesPagedResponse listTpuVms(String projectId, String zone)
38+
throws IOException {
39+
ListNodesPagedResponse response;
40+
// Initialize client that will be used to send requests. This client only needs to be created
41+
// once, and can be reused for multiple requests.
42+
try (TpuClient tpuClient = TpuClient.create()) {
43+
String parent = String.format("projects/%s/locations/%s", projectId, zone);
44+
45+
ListNodesRequest request = ListNodesRequest.newBuilder().setParent(parent).build();
46+
response = tpuClient.listNodes(request);
47+
}
48+
return response;
49+
}
50+
}
51+
//[END tpu_vm_list]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package tpu;
18+
19+
//[START tpu_vm_start]
20+
21+
import com.google.cloud.tpu.v2.Node;
22+
import com.google.cloud.tpu.v2.NodeName;
23+
import com.google.cloud.tpu.v2.StartNodeRequest;
24+
import com.google.cloud.tpu.v2.TpuClient;
25+
import java.io.IOException;
26+
import java.util.concurrent.ExecutionException;
27+
28+
public class StartTpuVm {
29+
30+
public static void main(String[] args)
31+
throws IOException, ExecutionException, InterruptedException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String projectId = "YOUR_PROJECT_ID";
34+
String zone = "europe-west4-a";
35+
String tpuVmName = "YOUR_TPU_NAME";
36+
37+
startTpuVm(projectId, zone, tpuVmName);
38+
}
39+
40+
// Starts a TPU VM with the specified name in the given project and zone.
41+
public static void startTpuVm(String projectId, String zone, String tpuVmName)
42+
throws IOException, ExecutionException, InterruptedException {
43+
// Initialize client that will be used to send requests. This client only needs to be created
44+
// once, and can be reused for multiple requests.
45+
try (TpuClient tpuClient = TpuClient.create()) {
46+
String nodeName = NodeName.of(projectId, zone, tpuVmName).toString();
47+
48+
StartNodeRequest request = StartNodeRequest.newBuilder().setName(nodeName).build();
49+
Node response = tpuClient.startNodeAsync(request).get();
50+
51+
System.out.printf("TPU VM started: %s\n", response.getName());
52+
}
53+
}
54+
}
55+
//[END tpu_vm_start]

0 commit comments

Comments
 (0)